Skip to contentSkip to conversation

CXL Region Creation / HDM decoder programming

RFC PATCH In review Ben Widawsky 3 participants 112 messages Oct 22, 2021 – Jan 6, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover CXL Region Creation / HDM decoder programming

Because v1 wasn't reviewed, and I quickly asked people to ignore it in favor of this version, I'm not going to bother listing out the changes. I've only issued minor edits to the original cover letter. If you already read that, feel free to move along.

I don't have plans to send further RFCs until I receive feedback, good or bad.

CXL region creation
-------------------
An interleaved set of devices is known in the Compute Express Link [1] specification as a region. In addition to a region being comprised of devices that can be configured in a variety of orders there are other properties that define a region. It is expected that a region may be created as part of provisioning by the hardware vendor, or interactively via operating system controls. This patch series implements both the interfaces to create and configure a region, as well as the algorithm to program the HDM decoders to enable CXL.mem traffic while obeying those configuration parameters. The series stops short of storing the new regions in the Label Storage Area of the CXL devices.

Some version of this functionality has all been posted previously, with the exception of the actual HDM decoder programming. It's probably wise to forget those exist, and take my apology in advance for not addressing feedback you may have already given.

There are two branches I am using as development branches. The branch for port/mem driver [2] is fairly solid. The branch for region creation [3] is less baked.

cxl_port
========

The cxl_port driver is implemented within the cxl_port module. While loading of this module is optional, the other new drivers depend on it. The port driver is responsible for all activities around HDM decoder enumeration and programming. Introduced earlier, the concept of a port is an abstraction over CXL components with an upstream port, every host bridge, switch, and endpoint.

cxl_mem
=======

The cxl_mem driver's main job is to walk up the hierarchy to make the determination if it is CXL.mem routed, meaning, all components above it in the hierarchy are participating in the CXL.mem protocol. It is implemented within the cxl_mem module. As the host bridge ports are added by a platform specific driver, such as cxl_acpi, the scope of the mem driver can be reduced to scan for switches and ask cxl_core to work on enumerating them. With this done, the determination as to whether a device is CXL.mem routed can be done simply by checking if the struct device has a driver bound to it.

This driver is also a logical place to migrate certain functionality from cxl_pci. That is saved for later.

cxl_region
==========

Region verification and programming state are owned by the cxl_region driver (implemented in the cxl_region module). It relies on cxl_mem to determine if devices are CXL routed, and cxl_port to actually handle the programming of the HDM decoders. Much of the region driver is an implementation of algorithms described in the CXL Type 3 Memory Device Software Guide [4].

The patches for the region driver could be squashed. They're broken out to aid review and because that's the order they were implemented in. My preference is to keep those as they are.

Why RFC?
--------
While I think most of the architecture is sound, I don't believe anyone but other developers should use this branch. Where I'd really like most eyes:
- Locking and device lifetimes. I suspect I have many bugs in this area.
- Region configuration. Should values have a default, should they all be explicit?
- What should/shouldn't be in core. I like how this ended up, but at this point I'm fairly biased (CXL.cache pun).
- What to extend to cxl_test.

What's missing
---------------
- CXL 2.0 switch support
- A full topology lock for programming HDM decoders. I'm looking for feedback on the best way to do this.
- Check that HDM decoder programming addresses are correct (must program higher addresses only)
- Volatile regions (or BIOS configured persistent regions)
- Connection to libnvdimm/labels. This includes many aspects, not the least of which is saving the region into the Label Storage Area so that it can be reestablished on reboot.

Here is an example of output when programming a x1 interleave region:

[   23.959814][  T645] cxl_core:cxl_add_region:406: cxl region0.0:0: Added region0.0:0 to decoder0.0
[   23.962972][  T645] cxl_port:cxl_commit_decoder:248: cxl_port port1: decoder1.0
[   23.962972][  T645] 	Base 0x0000004c00000000
[   23.962972][  T645] 	Size 268435456
[   23.962972][  T645] 	IG 256
[   23.962972][  T645] 	IW 1
[   23.962972][  T645] 	TargetList: 0 -1 -1 -1 -1 -1 -1 -1
[   23.965529][  T645] cxl_port:cxl_commit_decoder:248: cxl_port port3: decoder3.0
[   23.965529][  T645] 	Base 0x0000004c00000000
[   23.965529][  T645] 	Size 268435456
[   23.965529][  T645] 	IG 256
[   23.965529][  T645] 	IW 1
[   23.965529][  T645] 	TargetList: -1 -1 -1 -1 -1 -1 -1 -1

If you're wondering how I tested this, I've baked it into my cxlctl app [5] and lib [6]. Eventually this will get absorbed by ndctl/cxl-cli/libcxl [7]. Region deletion isn't implemented yet.

To get the detailed errors, trace-cmd can be utilized. Until a region device exists, the region module will not be loaded, which means the region tracepoints will not exist. To get around this, modprobe cxl_region before anything.

trace-cmd record -e cxl ./cxlctl create-region -n -a -s $((256<<20)) /sys/bus/cxl/devices/decoder0.0

Note: A minor bugfix is needed in QEMU if testing interleave configs. I've pushed that to my v4 branch.
---

[1]: https://www.computeexpresslink.org/download-the-specification
[2]: https://gitlab.com/bwidawsk/linux/-/tree/cxl_port-v3
[3]: https://gitlab.com/bwidawsk/linux/-/tree/cxl_regions-v4
[4]: https://cdrdv2.intel.com/v1/dl/getContent/643805?wapkw=CXL%20memory%20device%20sw%20guide
[5]: https://gitlab.com/bwidawsk-cxl/cxlctl
[6]: https://gitlab.com/bwidawsk-cxl/cxl_rs
[7]: https://lore.kernel.org/linux-cxl/CAPcyv4joKOhTdaRBJVeoOtqhRjBvdtt9902TS=c39=zWTZXvuw@mail.gmail.com/
---

Ben Widawsky (28):
cxl: Rename CXL_MEM to CXL_PCI
cxl: Move register block enumeration to core cxl/acpi: Map component registers for Root Ports
cxl: Add helper for new drivers
cxl/core: Convert decoder range to resource
cxl: Introduce endpoint decoders
cxl/core: Move target population locking to caller cxl/port: Introduce a port driver
cxl/acpi: Map single port host bridge component registers cxl/core: Store global list of root ports cxl/acpi: Rescan bus at probe completion cxl/core: Store component register base for memdevs
cxl: Flesh out register names
cxl: Hide devm host for ports
cxl/core: Introduce API to scan switch ports
cxl: Introduce cxl_mem driver
cxl: Disable switch hierarchies for now cxl/region: Add region creation ABI
cxl/region: Introduce concept of region configuration cxl/region: Introduce a cxl_region driver cxl/acpi: Handle address space allocation cxl/region: Address space allocation
cxl/region: Implement XHB verification cxl/region: HB port config verification cxl/region: Record host bridge target list cxl/mem: Store the endpoint's uport
cxl/region: Gather HDM decoder resources
cxl: Program decoders for regions

 .clang-format                                 |   3 +
 Documentation/ABI/testing/sysfs-bus-cxl       |  63 ++
 .../driver-api/cxl/memory-devices.rst         |  28 +
 drivers/cxl/Kconfig                           |  28 +-
 drivers/cxl/Makefile                          |   8 +-
 drivers/cxl/acpi.c                            | 132 +++-
 drivers/cxl/core/Makefile                     |   2 +
 drivers/cxl/core/bus.c                        | 437 +++++++++++-
 drivers/cxl/core/core.h                       |   2 +
 drivers/cxl/core/memdev.c                     |   7 +-
 drivers/cxl/core/pci.c                        |  99 +++
 drivers/cxl/core/region.c                     | 453 +++++++++++++
 drivers/cxl/core/regs.c                       |  62 +-
 drivers/cxl/cxl.h                             |  85 ++-
 drivers/cxl/cxlmem.h                          |   8 +-
 drivers/cxl/mem.c                             | 162 +++++
 drivers/cxl/pci.c                             |  69 +-
 drivers/cxl/pci.h                             |  48 +-
 drivers/cxl/port.c                            | 491 ++++++++++++++
 drivers/cxl/region.c                          | 629 ++++++++++++++++++
 drivers/cxl/region.h                          |  57 ++
 drivers/cxl/trace.h                           |  75 +++
 tools/testing/cxl/Kbuild                      |   2 +
 tools/testing/cxl/mock_acpi.c                 |   4 +-
 tools/testing/cxl/test/mem.c                  |   3 +-
 25 files changed, 2806 insertions(+), 151 deletions(-)
 create mode 100644 drivers/cxl/core/pci.c
 create mode 100644 drivers/cxl/core/region.c
 create mode 100644 drivers/cxl/mem.c
 create mode 100644 drivers/cxl/port.c
 create mode 100644 drivers/cxl/region.c
 create mode 100644 drivers/cxl/region.h
 create mode 100644 drivers/cxl/trace.h
#1 lore raw

RFC PATCH v2 1/28 cxl: Rename CXL_MEM to CXL_PCI

With the upcoming introduction of a driver to control the non-PCI aspects of CXL.mem, such as interleave set creation and configuration, there will be an opportunity to disconnection control over CXL device memory and CXL device manageability. CXL device manageability is implemented by the cxl_pci driver. Doing this rename allows the CXL memory driver to be enabled by a new config option independently of CXL device manageability through CXL.io/PCI mechanisms.

---
 drivers/cxl/Kconfig  | 13 ++++++-------
 drivers/cxl/Makefile |  2 +-
 2 files changed, 7 insertions(+), 8 deletions(-)
drivers/cxl/Kconfig +6 -7
index e6de221cc568..23773d0ac896 100644
@@ -13,14 +13,13 @@ menuconfig CXL_BUS
 
 if CXL_BUS
 
-config CXL_MEM
-	tristate "CXL.mem: Memory Devices"
+config CXL_PCI
+	tristate "PCI manageability"
 	default CXL_BUS
 	help
-	  The CXL.mem protocol allows a device to act as a provider of
-	  "System RAM" and/or "Persistent Memory" that is fully coherent
-	  as if the memory was attached to the typical CPU memory
-	  controller.
+	  The CXL specification defines a set of interfaces which are controlled
+	  through well known PCI configuration mechanisms. Such access is
+	  referred to CXL.io in the specification.
 
 	  Say 'y/m' to enable a driver that will attach to CXL.mem devices for
 	  configuration and management primarily via the mailbox interface. See
@@ -31,7 +30,7 @@ config CXL_MEM
 
 config CXL_MEM_RAW_COMMANDS
 	bool "RAW Command Interface for Memory Devices"
-	depends on CXL_MEM
+	depends on CXL_PCI
 	help
 	  Enable CXL RAW command interface.
 
drivers/cxl/Makefile +1 -1
index d1aaabc940f3..cf07ae6cea17 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
-obj-$(CONFIG_CXL_MEM) += cxl_pci.o
+obj-$(CONFIG_CXL_PCI) += cxl_pci.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 
Signature · 1 line
2.33.1
#2 lore raw

Re: [RFC PATCH v2 01/28] cxl: Rename CXL_MEM to CXL_PCI

Quoted 4 lines · Ben Widawsky

With the upcoming introduction of a driver to control the non-PCI aspects of CXL.mem, such as interleave set creation and configuration, there will be an opportunity to disconnection control over CXL device

s/disconnection/disconnect/

Quoted 4 lines

memory and CXL device manageability. CXL device manageability is implemented by the cxl_pci driver. Doing this rename allows the CXL memory driver to be enabled by a new config option independently of CXL device manageability through CXL.io/PCI mechanisms.

That comes across a bit hard to parse to me, how about:

"The cxl_mem module was renamed cxl_pci in commit 21e9f76733a8 ("cxl:
Rename mem to pci"). In preparation for adding an ancillary driver for cxl_memdev devices (registered on the cxl bus by cxl_pci), go ahead and rename CONFIG_CXL_MEM to CONFIG_CXL_PCI. Tree up the CXL_MEM name for that new driver to manage generic CXL.mem endpoint operations."

Quoted 12 lines
---
 drivers/cxl/Kconfig  | 13 ++++++-------
 drivers/cxl/Makefile |  2 +-
 2 files changed, 7 insertions(+), 8 deletions(-)
drivers/cxl/Kconfig +0 -0
index e6de221cc568..23773d0ac896 100644

drivers/cxl/Kconfig menuconfig CXL_BUS · line 17

Quoted 7 lines
drivers/cxl/Kconfig +2 -2
@@ -13,14 +13,13 @@ menuconfig CXL_BUS
 
 if CXL_BUS
 
-config CXL_MEM
-       tristate "CXL.mem: Memory Devices"
+config CXL_PCI
+       tristate "PCI manageability"

s/PCI manageability/CXL Memory Device: PCI Operations/

...as I don't think an end user reading "PCI Manageability" would know that it supports basic memory expander enumeration and mailbox operations.

drivers/cxl/Kconfig menuconfig CXL_BUS · line 22

Quoted 9 lines
drivers/cxl/Kconfig +3 -4
        default CXL_BUS
        help
-         The CXL.mem protocol allows a device to act as a provider of
-         "System RAM" and/or "Persistent Memory" that is fully coherent
-         as if the memory was attached to the typical CPU memory
-         controller.
+         The CXL specification defines a set of interfaces which are controlled
+         through well known PCI configuration mechanisms. Such access is
+         referred to CXL.io in the specification.

The CXL specification defines a "CXL memory device" sub-class in the PCI "memory controller" base class of devices. Device's identified by this class code provide support for volatile and / or persistent memory to be mapped into the system address map (Host-managed Device Memory (HDM)).

drivers/cxl/Kconfig menuconfig CXL_BUS · line 24

Quoted 2 lines
drivers/cxl/Kconfig +0 -0
          Say 'y/m' to enable a driver that will attach to CXL.mem devices for

This would need updating too...

s/CXL.mem devices /generic CXL memory expanders identified by the memory device class code/

...to reduce confusion about this driver for generic type-3 vs vendor specific type-2 devices that also support CXL.mem

Quoted 25 lines
drivers/cxl/Kconfig +1 -1
          configuration and management primarily via the mailbox interface. See
@@ -31,7 +30,7 @@ config CXL_MEM
 
 config CXL_MEM_RAW_COMMANDS
        bool "RAW Command Interface for Memory Devices"
-       depends on CXL_MEM
+       depends on CXL_PCI
        help
          Enable CXL RAW command interface.
 
drivers/cxl/Makefile +1 -2
index d1aaabc940f3..cf07ae6cea17 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
-obj-$(CONFIG_CXL_MEM) += cxl_pci.o
+obj-$(CONFIG_CXL_PCI) += cxl_pci.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 
--

2.33.1

#3 lore raw

Re: [RFC PATCH v2 01/28] cxl: Rename CXL_MEM to CXL_PCI

Quoted 13 lines · Dan Williams
Quoted 4 lines · Ben Widawsky

With the upcoming introduction of a driver to control the non-PCI aspects of CXL.mem, such as interleave set creation and configuration, there will be an opportunity to disconnection control over CXL device

s/disconnection/disconnect/

Quoted 4 lines

memory and CXL device manageability. CXL device manageability is implemented by the cxl_pci driver. Doing this rename allows the CXL memory driver to be enabled by a new config option independently of CXL device manageability through CXL.io/PCI mechanisms.

Quoted 6 lines

That comes across a bit hard to parse to me, how about:

"The cxl_mem module was renamed cxl_pci in commit 21e9f76733a8 ("cxl:
Rename mem to pci"). In preparation for adding an ancillary driver for cxl_memdev devices (registered on the cxl bus by cxl_pci), go ahead and rename CONFIG_CXL_MEM to CONFIG_CXL_PCI. Tree up the CXL_MEM name

I assume you meant s/tree/tee - right?

Quoted 47 lines

for that new driver to manage generic CXL.mem endpoint operations."

Quoted 12 lines
---
 drivers/cxl/Kconfig  | 13 ++++++-------
 drivers/cxl/Makefile |  2 +-
 2 files changed, 7 insertions(+), 8 deletions(-)
drivers/cxl/Kconfig +0 -0
index e6de221cc568..23773d0ac896 100644

drivers/cxl/Kconfig menuconfig CXL_BUS · line 17

Quoted 7 lines
drivers/cxl/Kconfig +2 -2
@@ -13,14 +13,13 @@ menuconfig CXL_BUS
 
 if CXL_BUS
 
-config CXL_MEM
-       tristate "CXL.mem: Memory Devices"
+config CXL_PCI
+       tristate "PCI manageability"

s/PCI manageability/CXL Memory Device: PCI Operations/

...as I don't think an end user reading "PCI Manageability" would know that it supports basic memory expander enumeration and mailbox operations.

drivers/cxl/Kconfig menuconfig CXL_BUS · line 22

Quoted 9 lines
drivers/cxl/Kconfig +3 -4
        default CXL_BUS
        help
-         The CXL.mem protocol allows a device to act as a provider of
-         "System RAM" and/or "Persistent Memory" that is fully coherent
-         as if the memory was attached to the typical CPU memory
-         controller.
+         The CXL specification defines a set of interfaces which are controlled
+         through well known PCI configuration mechanisms. Such access is
+         referred to CXL.io in the specification.

The CXL specification defines a "CXL memory device" sub-class in the PCI "memory controller" base class of devices. Device's identified by this class code provide support for volatile and / or persistent memory to be mapped into the system address map (Host-managed Device Memory (HDM)).

drivers/cxl/Kconfig menuconfig CXL_BUS · line 24

Quoted 2 lines
drivers/cxl/Kconfig +0 -0
          Say 'y/m' to enable a driver that will attach to CXL.mem devices for
Quoted 8 lines

This would need updating too...

s/CXL.mem devices /generic CXL memory expanders identified by the memory device class code/

...to reduce confusion about this driver for generic type-3 vs vendor specific type-2 devices that also support CXL.mem

Overall seems like an improvement to me. I'm not too fond of the word "generic" though, both here and in the commit message. I think they work equally well just deleting that word.

Quoted 25 lines
Quoted 25 lines
drivers/cxl/Kconfig +1 -1
          configuration and management primarily via the mailbox interface. See
@@ -31,7 +30,7 @@ config CXL_MEM
 
 config CXL_MEM_RAW_COMMANDS
        bool "RAW Command Interface for Memory Devices"
-       depends on CXL_MEM
+       depends on CXL_PCI
        help
          Enable CXL RAW command interface.
 
drivers/cxl/Makefile +1 -2
index d1aaabc940f3..cf07ae6cea17 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
-obj-$(CONFIG_CXL_MEM) += cxl_pci.o
+obj-$(CONFIG_CXL_PCI) += cxl_pci.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 
--

2.33.1

#4 lore raw

Re: [RFC PATCH v2 01/28] cxl: Rename CXL_MEM to CXL_PCI

Quoted 15 lines · Ben Widawsky
Quoted 13 lines · Dan Williams
Quoted 4 lines · Ben Widawsky

With the upcoming introduction of a driver to control the non-PCI aspects of CXL.mem, such as interleave set creation and configuration, there will be an opportunity to disconnection control over CXL device

s/disconnection/disconnect/

Quoted 4 lines

memory and CXL device manageability. CXL device manageability is implemented by the cxl_pci driver. Doing this rename allows the CXL memory driver to be enabled by a new config option independently of CXL device manageability through CXL.io/PCI mechanisms.

Quoted 8 lines
Quoted 6 lines

That comes across a bit hard to parse to me, how about:

"The cxl_mem module was renamed cxl_pci in commit 21e9f76733a8 ("cxl:
Rename mem to pci"). In preparation for adding an ancillary driver for cxl_memdev devices (registered on the cxl bus by cxl_pci), go ahead and rename CONFIG_CXL_MEM to CONFIG_CXL_PCI. Tree up the CXL_MEM name

I assume you meant s/tree/tee - right?

Sorry, that should have been "Free"

Quoted 53 lines
Quoted 53 lines

for that new driver to manage generic CXL.mem endpoint operations."

Quoted 12 lines
---
 drivers/cxl/Kconfig  | 13 ++++++-------
 drivers/cxl/Makefile |  2 +-
 2 files changed, 7 insertions(+), 8 deletions(-)
drivers/cxl/Kconfig +0 -0
index e6de221cc568..23773d0ac896 100644

drivers/cxl/Kconfig menuconfig CXL_BUS · line 17

Quoted 7 lines
drivers/cxl/Kconfig +2 -2
@@ -13,14 +13,13 @@ menuconfig CXL_BUS
 
 if CXL_BUS
 
-config CXL_MEM
-       tristate "CXL.mem: Memory Devices"
+config CXL_PCI
+       tristate "PCI manageability"

s/PCI manageability/CXL Memory Device: PCI Operations/

...as I don't think an end user reading "PCI Manageability" would know that it supports basic memory expander enumeration and mailbox operations.

drivers/cxl/Kconfig menuconfig CXL_BUS · line 22

Quoted 9 lines
drivers/cxl/Kconfig +3 -4
        default CXL_BUS
        help
-         The CXL.mem protocol allows a device to act as a provider of
-         "System RAM" and/or "Persistent Memory" that is fully coherent
-         as if the memory was attached to the typical CPU memory
-         controller.
+         The CXL specification defines a set of interfaces which are controlled
+         through well known PCI configuration mechanisms. Such access is
+         referred to CXL.io in the specification.

The CXL specification defines a "CXL memory device" sub-class in the PCI "memory controller" base class of devices. Device's identified by this class code provide support for volatile and / or persistent memory to be mapped into the system address map (Host-managed Device Memory (HDM)).

drivers/cxl/Kconfig menuconfig CXL_BUS · line 24

Quoted 2 lines
drivers/cxl/Kconfig +0 -0
          Say 'y/m' to enable a driver that will attach to CXL.mem devices for

This would need updating too...

s/CXL.mem devices /generic CXL memory expanders identified by the memory device class code/

Quoted 7 lines
Quoted 3 lines

...to reduce confusion about this driver for generic type-3 vs vendor specific type-2 devices that also support CXL.mem

Overall seems like an improvement to me. I'm not too fond of the word "generic" though, both here and in the commit message. I think they work equally well just deleting that word.

Sure, and it's redundant with the mention of "class code".

#5 lore raw

RFC PATCH v2 2/28 cxl: Move register block enumeration to core

In reply to Ben Widawsky

CXL drivers or cxl_core itself will require the ability to map component registers in order to map HDM decoder resources amongst other things. Much of the register mapping code has already moved into cxl_core. The code to pull the BAR number and block office remained within cxl_pci because there was no need to move it. Upcoming work will require this functionality to be available outside of cxl_pci.

---
 drivers/cxl/core/regs.c | 54 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h       |  4 +++
 drivers/cxl/pci.c       | 52 ---------------------------------------
 3 files changed, 58 insertions(+), 52 deletions(-)
drivers/cxl/core/regs.c +54 -0
index 41de4a136ecd..40598905c080 100644
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/pci.h>
 #include <cxlmem.h>
+#include <pci.h>
 
 /**
  * DOC: cxl registers
@@ -247,3 +248,56 @@ int cxl_map_device_regs(struct pci_dev *pdev,
 	return 0;
 }
 EXPORT_SYMBOL_GPL(cxl_map_device_regs);
+
+static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
+				struct cxl_register_map *map)
+{
+	map->block_offset =
+		((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
+	map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
+	map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
+}
+
+/**
+ * cxl_find_regblock() - Locate register blocks by type
+ * @pdev: The CXL PCI device to enumerate.
+ * @type: Register Block Indicator id
+ * @map: Enumeration output, clobbered on error
+ *
+ * Return: 0 if register block enumerated, negative error code otherwise
+ *
+ * A CXL DVSEC may additional point one or more register blocks, search
+ * for them by @type.
+ */
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+		      struct cxl_register_map *map)
+{
+	u32 regloc_size, regblocks;
+	int regloc, i;
+
+	regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
+					   PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+	if (!regloc)
+		return -ENXIO;
+
+	pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
+	regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
+
+	regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
+	regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+
+	for (i = 0; i < regblocks; i++, regloc += 8) {
+		u32 reg_lo, reg_hi;
+
+		pci_read_config_dword(pdev, regloc, &reg_lo);
+		pci_read_config_dword(pdev, regloc + 4, &reg_hi);
+
+		cxl_decode_regblock(reg_lo, reg_hi, map);
+
+		if (map->reg_type == type)
+			return 0;
+	}
+
+	return -ENODEV;
+}
+EXPORT_SYMBOL_GPL(cxl_find_regblock);
drivers/cxl/cxl.h +4 -0
index 7cd16ef144dd..f06c596fad71 100644
@@ -161,6 +161,10 @@ int cxl_map_device_regs(struct pci_dev *pdev,
 			struct cxl_device_regs *regs,
 			struct cxl_register_map *map);
 
+enum cxl_regloc_type;
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+		      struct cxl_register_map *map);
+
 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
 #define CXL_TARGET_STRLEN 20
 
drivers/cxl/pci.c +0 -52

58 lines not on this page · raw message

Signature · 1 line
2.33.1
#6 lore raw

Re: [RFC PATCH v2 02/28] cxl: Move register block enumeration to core

Quoted 3 lines · Ben Widawsky

CXL drivers or cxl_core itself will require the ability to map component registers in order to map HDM decoder resources amongst other things.

The PCI bits of this are only needed for switch support, right? Everything else already knows how to find the component_reg_phys base before devm_cxl_add_port().

It would help if this first sentence was a specific example where the PCI helpers are needed outside of cxl_pci.

Quoted 162 lines

Much of the register mapping code has already moved into cxl_core. The code to pull the BAR number and block office remained within cxl_pci because there was no need to move it. Upcoming work will require this functionality to be available outside of cxl_pci.

---
 drivers/cxl/core/regs.c | 54 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h       |  4 +++
 drivers/cxl/pci.c       | 52 ---------------------------------------
 3 files changed, 58 insertions(+), 52 deletions(-)
drivers/cxl/core/regs.c +54 -0
index 41de4a136ecd..40598905c080 100644
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/pci.h>
 #include <cxlmem.h>
+#include <pci.h>
 
 /**
  * DOC: cxl registers
@@ -247,3 +248,56 @@ int cxl_map_device_regs(struct pci_dev *pdev,
        return 0;
 }
 EXPORT_SYMBOL_GPL(cxl_map_device_regs);
+
+static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
+                               struct cxl_register_map *map)
+{
+       map->block_offset =
+               ((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
+       map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
+       map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
+}
+
+/**
+ * cxl_find_regblock() - Locate register blocks by type
+ * @pdev: The CXL PCI device to enumerate.
+ * @type: Register Block Indicator id
+ * @map: Enumeration output, clobbered on error
+ *
+ * Return: 0 if register block enumerated, negative error code otherwise
+ *
+ * A CXL DVSEC may additional point one or more register blocks, search
+ * for them by @type.
+ */
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+                     struct cxl_register_map *map)
+{
+       u32 regloc_size, regblocks;
+       int regloc, i;
+
+       regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
+                                          PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+       if (!regloc)
+               return -ENXIO;
+
+       pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
+       regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
+
+       regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
+       regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+
+       for (i = 0; i < regblocks; i++, regloc += 8) {
+               u32 reg_lo, reg_hi;
+
+               pci_read_config_dword(pdev, regloc, &reg_lo);
+               pci_read_config_dword(pdev, regloc + 4, &reg_hi);
+
+               cxl_decode_regblock(reg_lo, reg_hi, map);
+
+               if (map->reg_type == type)
+                       return 0;
+       }
+
+       return -ENODEV;
+}
+EXPORT_SYMBOL_GPL(cxl_find_regblock);
drivers/cxl/cxl.h +4 -0
index 7cd16ef144dd..f06c596fad71 100644
@@ -161,6 +161,10 @@ int cxl_map_device_regs(struct pci_dev *pdev,
                        struct cxl_device_regs *regs,
                        struct cxl_register_map *map);
 
+enum cxl_regloc_type;
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+                     struct cxl_register_map *map);
+
 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
 #define CXL_TARGET_STRLEN 20
 
drivers/cxl/pci.c +0 -53
index f19a06809079..d1adc759d051 100644
@@ -400,58 +400,6 @@ static int cxl_map_regs(struct cxl_mem *cxlm, struct cxl_register_map *map)
        return 0;
 }
 
-static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
-                               struct cxl_register_map *map)
-{
-       map->block_offset =
-               ((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
-       map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
-       map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
-}
-
-/**
- * cxl_find_regblock() - Locate register blocks by type
- * @pdev: The CXL PCI device to enumerate.
- * @type: Register Block Indicator id
- * @map: Enumeration output, clobbered on error
- *
- * Return: 0 if register block enumerated, negative error code otherwise
- *
- * A CXL DVSEC may additional point one or more register blocks, search
- * for them by @type.
- */
-static int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
-                            struct cxl_register_map *map)
-{
-       u32 regloc_size, regblocks;
-       int regloc, i;
-
-       regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
-                                          PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
-       if (!regloc)
-               return -ENXIO;
-
-       pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
-       regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
-
-       regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
-       regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
-
-       for (i = 0; i < regblocks; i++, regloc += 8) {
-               u32 reg_lo, reg_hi;
-
-               pci_read_config_dword(pdev, regloc, &reg_lo);
-               pci_read_config_dword(pdev, regloc + 4, &reg_hi);
-
-               cxl_decode_regblock(reg_lo, reg_hi, map);
-
-               if (map->reg_type == type)
-                       return 0;
-       }
-
-       return -ENODEV;
-}
-
 static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
                          struct cxl_register_map *map)
 {
--

2.33.1

#7 lore raw

Re: [RFC PATCH v2 02/28] cxl: Move register block enumeration to core

Quoted 5 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

CXL drivers or cxl_core itself will require the ability to map component registers in order to map HDM decoder resources amongst other things.

Quoted 7 lines

The PCI bits of this are only needed for switch support, right? Everything else already knows how to find the component_reg_phys base before devm_cxl_add_port().

It would help if this first sentence was a specific example where the PCI helpers are needed outside of cxl_pci.

You've already spotted the other use in the next commit. I can change "CXL drivers" to the specific drivers and related components.

Quoted 162 lines
Quoted 162 lines

Much of the register mapping code has already moved into cxl_core. The code to pull the BAR number and block office remained within cxl_pci because there was no need to move it. Upcoming work will require this functionality to be available outside of cxl_pci.

---
 drivers/cxl/core/regs.c | 54 +++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h       |  4 +++
 drivers/cxl/pci.c       | 52 ---------------------------------------
 3 files changed, 58 insertions(+), 52 deletions(-)
drivers/cxl/core/regs.c +54 -0
index 41de4a136ecd..40598905c080 100644
@@ -5,6 +5,7 @@
 #include <linux/slab.h>
 #include <linux/pci.h>
 #include <cxlmem.h>
+#include <pci.h>
 
 /**
  * DOC: cxl registers
@@ -247,3 +248,56 @@ int cxl_map_device_regs(struct pci_dev *pdev,
        return 0;
 }
 EXPORT_SYMBOL_GPL(cxl_map_device_regs);
+
+static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
+                               struct cxl_register_map *map)
+{
+       map->block_offset =
+               ((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
+       map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
+       map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
+}
+
+/**
+ * cxl_find_regblock() - Locate register blocks by type
+ * @pdev: The CXL PCI device to enumerate.
+ * @type: Register Block Indicator id
+ * @map: Enumeration output, clobbered on error
+ *
+ * Return: 0 if register block enumerated, negative error code otherwise
+ *
+ * A CXL DVSEC may additional point one or more register blocks, search
+ * for them by @type.
+ */
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+                     struct cxl_register_map *map)
+{
+       u32 regloc_size, regblocks;
+       int regloc, i;
+
+       regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
+                                          PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+       if (!regloc)
+               return -ENXIO;
+
+       pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
+       regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
+
+       regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
+       regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+
+       for (i = 0; i < regblocks; i++, regloc += 8) {
+               u32 reg_lo, reg_hi;
+
+               pci_read_config_dword(pdev, regloc, &reg_lo);
+               pci_read_config_dword(pdev, regloc + 4, &reg_hi);
+
+               cxl_decode_regblock(reg_lo, reg_hi, map);
+
+               if (map->reg_type == type)
+                       return 0;
+       }
+
+       return -ENODEV;
+}
+EXPORT_SYMBOL_GPL(cxl_find_regblock);
drivers/cxl/cxl.h +4 -0
index 7cd16ef144dd..f06c596fad71 100644
@@ -161,6 +161,10 @@ int cxl_map_device_regs(struct pci_dev *pdev,
                        struct cxl_device_regs *regs,
                        struct cxl_register_map *map);
 
+enum cxl_regloc_type;
+int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
+                     struct cxl_register_map *map);
+
 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
 #define CXL_TARGET_STRLEN 20
 
drivers/cxl/pci.c +0 -53
index f19a06809079..d1adc759d051 100644
@@ -400,58 +400,6 @@ static int cxl_map_regs(struct cxl_mem *cxlm, struct cxl_register_map *map)
        return 0;
 }
 
-static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
-                               struct cxl_register_map *map)
-{
-       map->block_offset =
-               ((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
-       map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
-       map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
-}
-
-/**
- * cxl_find_regblock() - Locate register blocks by type
- * @pdev: The CXL PCI device to enumerate.
- * @type: Register Block Indicator id
- * @map: Enumeration output, clobbered on error
- *
- * Return: 0 if register block enumerated, negative error code otherwise
- *
- * A CXL DVSEC may additional point one or more register blocks, search
- * for them by @type.
- */
-static int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
-                            struct cxl_register_map *map)
-{
-       u32 regloc_size, regblocks;
-       int regloc, i;
-
-       regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
-                                          PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
-       if (!regloc)
-               return -ENXIO;
-
-       pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
-       regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
-
-       regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
-       regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
-
-       for (i = 0; i < regblocks; i++, regloc += 8) {
-               u32 reg_lo, reg_hi;
-
-               pci_read_config_dword(pdev, regloc, &reg_lo);
-               pci_read_config_dword(pdev, regloc + 4, &reg_hi);
-
-               cxl_decode_regblock(reg_lo, reg_hi, map);
-
-               if (map->reg_type == type)
-                       return 0;
-       }
-
-       return -ENODEV;
-}
-
 static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
                          struct cxl_register_map *map)
 {
--

2.33.1

#8 lore raw

RFC PATCH v2 3/28 cxl/acpi: Map component registers for Root Ports

In reply to Ben Widawsky

With the addition of cxl_find_register_block() in cxl_core, it becomes trivial to complete the TODO left for mapping the component registers of root ports. None of the CXL drivers currently use component registers of downstream ports (which is what a CXL 2.0 Root Port is). As such, there should be no functional change.

---
 drivers/cxl/acpi.c | 10 ++++++++--
 drivers/cxl/pci.h  |  4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/cxl/acpi.c +8 -2
index af1c6c1875ac..7d13e7f0aefc 100644
@@ -7,6 +7,7 @@
 #include <linux/acpi.h>
 #include <linux/pci.h>
 #include "cxl.h"
+#include "pci.h"
 
 static struct acpi_table_header *acpi_cedt;
 
@@ -206,11 +207,13 @@ static resource_size_t get_chbcr(struct acpi_cedt_chbs *chbs)
 
 __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
 {
+	resource_size_t creg = CXL_RESOURCE_NONE;
 	struct cxl_walk_context *ctx = data;
 	struct pci_bus *root_bus = ctx->root;
 	struct cxl_port *port = ctx->port;
 	int type = pci_pcie_type(pdev);
 	struct device *dev = ctx->dev;
+	struct cxl_register_map map;
 	u32 lnkcap, port_num;
 	int rc;
 
@@ -224,9 +227,12 @@ __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
 				  &lnkcap) != PCIBIOS_SUCCESSFUL)
 		return 0;
 
-	/* TODO walk DVSEC to find component register base */
+	rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+	if (!rc)
+		creg = cxl_reg_block(pdev, &map);
+
 	port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
-	rc = cxl_add_dport(port, &pdev->dev, port_num, CXL_RESOURCE_NONE);
+	rc = cxl_add_dport(port, &pdev->dev, port_num, creg);
 	if (rc) {
 		ctx->error = rc;
 		return rc;
drivers/cxl/pci.h +4 -0
index 7d3e4bf06b45..12fdcb1b14e5 100644
@@ -31,4 +31,8 @@ enum cxl_regloc_type {
 #define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
 #define CXL_REGLOC_ADDR_MASK GENMASK(31, 16)
 
+#define cxl_reg_block(pdev, map)                                               \
+	((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
+			   (map)->block_offset))
+
 #endif /* __CXL_PCI_H__ */
Signature · 1 line
2.33.1
#9 lore raw

Re: [RFC PATCH v2 03/28] cxl/acpi: Map component registers for Root Ports

Quoted 6 lines · Ben Widawsky

With the addition of cxl_find_register_block() in cxl_core, it becomes trivial to complete the TODO left for mapping the component registers of root ports. None of the CXL drivers currently use component registers of downstream ports (which is what a CXL 2.0 Root Port is). As such, there should be no functional change.

Oh, I forgot that cxl_acpi had this todo, I think it would be best to fold this with the previous patch so that it makes a complete story.

Quoted 64 lines
---
 drivers/cxl/acpi.c | 10 ++++++++--
 drivers/cxl/pci.h  |  4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/cxl/acpi.c +8 -2
index af1c6c1875ac..7d13e7f0aefc 100644
@@ -7,6 +7,7 @@
 #include <linux/acpi.h>
 #include <linux/pci.h>
 #include "cxl.h"
+#include "pci.h"
 
 static struct acpi_table_header *acpi_cedt;
 
@@ -206,11 +207,13 @@ static resource_size_t get_chbcr(struct acpi_cedt_chbs *chbs)
 
 __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
 {
+       resource_size_t creg = CXL_RESOURCE_NONE;
        struct cxl_walk_context *ctx = data;
        struct pci_bus *root_bus = ctx->root;
        struct cxl_port *port = ctx->port;
        int type = pci_pcie_type(pdev);
        struct device *dev = ctx->dev;
+       struct cxl_register_map map;
        u32 lnkcap, port_num;
        int rc;
 
@@ -224,9 +227,12 @@ __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
                                  &lnkcap) != PCIBIOS_SUCCESSFUL)
                return 0;
 
-       /* TODO walk DVSEC to find component register base */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+       if (!rc)
+               creg = cxl_reg_block(pdev, &map);
+
        port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
-       rc = cxl_add_dport(port, &pdev->dev, port_num, CXL_RESOURCE_NONE);
+       rc = cxl_add_dport(port, &pdev->dev, port_num, creg);
        if (rc) {
                ctx->error = rc;
                return rc;
drivers/cxl/pci.h +4 -1
index 7d3e4bf06b45..12fdcb1b14e5 100644
@@ -31,4 +31,8 @@ enum cxl_regloc_type {
 #define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
 #define CXL_REGLOC_ADDR_MASK GENMASK(31, 16)
 
+#define cxl_reg_block(pdev, map)                                               \
+       ((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
+                          (map)->block_offset))
+
 #endif /* __CXL_PCI_H__ */
--

2.33.1

#10 lore raw

RFC PATCH v2 4/28 cxl: Add helper for new drivers

In reply to Ben Widawsky

Many CXL drivers simply want to register and unregister themselves. module_driver already supported this. A simple wrapper around that reduces a decent amount of boilerplate in upcoming patches.

---
 drivers/cxl/cxl.h | 3 +++
 1 file changed, 3 insertions(+)
drivers/cxl/cxl.h +3 -0
index f06c596fad71..4483e1a39fc3 100644
@@ -317,6 +317,9 @@ int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
 #define cxl_driver_register(x) __cxl_driver_register(x, THIS_MODULE, KBUILD_MODNAME)
 void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 
+#define module_cxl_driver(__cxl_driver) \
+	module_driver(__cxl_driver, cxl_driver_register, cxl_driver_unregister)
+
 #define CXL_DEVICE_NVDIMM_BRIDGE	1
 #define CXL_DEVICE_NVDIMM		2
 
Signature · 1 line
2.33.1
#11 lore raw

Re: [RFC PATCH v2 04/28] cxl: Add helper for new drivers

s/cxl: Add helper for new drivers/cxl: Introduce module_cxl_driver/

...as "Add helper for new drivers" could be any kind of helper anywhere in the stack.

Quoted 5 lines · Ben Widawsky

Many CXL drivers simply want to register and unregister themselves. module_driver already supported this. A simple wrapper around that reduces a decent amount of boilerplate in upcoming patches.

Other than the subject clarification, looks good to me.

Quoted 23 lines
---
 drivers/cxl/cxl.h | 3 +++
 1 file changed, 3 insertions(+)
drivers/cxl/cxl.h +3 -1
index f06c596fad71..4483e1a39fc3 100644
@@ -317,6 +317,9 @@ int __cxl_driver_register(struct cxl_driver *cxl_drv, struct module *owner,
 #define cxl_driver_register(x) __cxl_driver_register(x, THIS_MODULE, KBUILD_MODNAME)
 void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 
+#define module_cxl_driver(__cxl_driver) \
+       module_driver(__cxl_driver, cxl_driver_register, cxl_driver_unregister)
+
 #define CXL_DEVICE_NVDIMM_BRIDGE       1
 #define CXL_DEVICE_NVDIMM              2
 
--

2.33.1

#12 lore raw

RFC PATCH v2 5/28 cxl/core: Convert decoder range to resource

In reply to Ben Widawsky

Regions will use the resource API in order to help manage allocated space. As regions are children of the decoder, it makes sense that the parent host the main resource to be suballocated by the region.

---
 drivers/cxl/acpi.c     | 12 ++++--------
 drivers/cxl/core/bus.c |  4 ++--
 drivers/cxl/cxl.h      |  4 ++--
 3 files changed, 8 insertions(+), 12 deletions(-)
drivers/cxl/acpi.c +4 -8
index 7d13e7f0aefc..b972abc9f6ef 100644
@@ -126,10 +126,9 @@ static void cxl_add_cfmws_decoders(struct device *dev,
 
 		cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
 		cxld->target_type = CXL_DECODER_EXPANDER;
-		cxld->range = (struct range) {
-			.start = cfmws->base_hpa,
-			.end = cfmws->base_hpa + cfmws->window_size - 1,
-		};
+		cxld->res = (struct resource)DEFINE_RES_MEM_NAMED(cfmws->base_hpa,
+								  cfmws->window_size,
+								  "cfmws");
 		cxld->interleave_ways = CFMWS_INTERLEAVE_WAYS(cfmws);
 		cxld->interleave_granularity =
 			CFMWS_INTERLEAVE_GRANULARITY(cfmws);
@@ -339,10 +338,7 @@ static int add_host_bridge_uport(struct device *match, void *arg)
 	cxld->interleave_ways = 1;
 	cxld->interleave_granularity = PAGE_SIZE;
 	cxld->target_type = CXL_DECODER_EXPANDER;
-	cxld->range = (struct range) {
-		.start = 0,
-		.end = -1,
-	};
+	cxld->res = (struct resource)DEFINE_RES_MEM(0, 0);
 
 	device_lock(&port->dev);
 	dport = list_first_entry(&port->dports, typeof(*dport), list);
drivers/cxl/core/bus.c +2 -2
index ebd061d03950..454d4d846eb2 100644
@@ -47,7 +47,7 @@ static ssize_t start_show(struct device *dev, struct device_attribute *attr,
 {
 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
 
-	return sysfs_emit(buf, "%#llx\n", cxld->range.start);
+	return sysfs_emit(buf, "%#llx\n", cxld->res.start);
 }
 static DEVICE_ATTR_RO(start);
 
@@ -56,7 +56,7 @@ static ssize_t size_show(struct device *dev, struct device_attribute *attr,
 {
 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
 
-	return sysfs_emit(buf, "%#llx\n", range_len(&cxld->range));
+	return sysfs_emit(buf, "%#llx\n", resource_size(&cxld->res));
 }
 static DEVICE_ATTR_RO(size);
 
drivers/cxl/cxl.h +2 -2
index 4483e1a39fc3..7f2e2bdc7883 100644
@@ -195,7 +195,7 @@ enum cxl_decoder_type {
  * struct cxl_decoder - CXL address range decode configuration
  * @dev: this decoder's device
  * @id: kernel device name id
- * @range: address range considered by this decoder
+ * @res: address space resources considered by this decoder
  * @interleave_ways: number of cxl_dports in this decode
  * @interleave_granularity: data stride per dport
  * @target_type: accelerator vs expander (type2 vs type3) selector
@@ -206,7 +206,7 @@ enum cxl_decoder_type {
 struct cxl_decoder {
 	struct device dev;
 	int id;
-	struct range range;
+	struct resource res;
 	int interleave_ways;
 	int interleave_granularity;
 	enum cxl_decoder_type target_type;
Signature · 1 line
2.33.1
#13 lore raw

Re: [RFC PATCH v2 05/28] cxl/core: Convert decoder range to resource

Quoted 17 lines · Ben Widawsky

Regions will use the resource API in order to help manage allocated space. As regions are children of the decoder, it makes sense that the parent host the main resource to be suballocated by the region.

---
 drivers/cxl/acpi.c     | 12 ++++--------
 drivers/cxl/core/bus.c |  4 ++--
 drivers/cxl/cxl.h      |  4 ++--
 3 files changed, 8 insertions(+), 12 deletions(-)
drivers/cxl/acpi.c +0 -0
index 7d13e7f0aefc..b972abc9f6ef 100644

drivers/cxl/acpi.c static void cxl_add_cfmws_decoders(struct device *dev, · line 131

Quoted 10 lines
drivers/cxl/acpi.c +3 -4
@@ -126,10 +126,9 @@ static void cxl_add_cfmws_decoders(struct device *dev,
 
                cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
                cxld->target_type = CXL_DECODER_EXPANDER;
-               cxld->range = (struct range) {
-                       .start = cfmws->base_hpa,
-                       .end = cfmws->base_hpa + cfmws->window_size - 1,
-               };
+               cxld->res = (struct resource)DEFINE_RES_MEM_NAMED(cfmws->base_hpa,
+                                                                 cfmws->window_size,
+                                                                 "cfmws");

I think this should just be DEFINE_RES_MEM(), and then set the name of it inside cxl_decoder_add() to the dev_name() of the decoder device. That way a dump of the resource tree hierarchy makes sense compared to the device hierarchy with actual device names not a series of repeating "cfmws" entries.

Quoted 29 lines
drivers/cxl/acpi.c +1 -4
                cxld->interleave_ways = CFMWS_INTERLEAVE_WAYS(cfmws);
                cxld->interleave_granularity =
                        CFMWS_INTERLEAVE_GRANULARITY(cfmws);
@@ -339,10 +338,7 @@ static int add_host_bridge_uport(struct device *match, void *arg)
        cxld->interleave_ways = 1;
        cxld->interleave_granularity = PAGE_SIZE;
        cxld->target_type = CXL_DECODER_EXPANDER;
-       cxld->range = (struct range) {
-               .start = 0,
-               .end = -1,
-       };
+       cxld->res = (struct resource)DEFINE_RES_MEM(0, 0);
 
        device_lock(&port->dev);
        dport = list_first_entry(&port->dports, typeof(*dport), list);
drivers/cxl/core/bus.c +1 -1
index ebd061d03950..454d4d846eb2 100644
@@ -47,7 +47,7 @@ static ssize_t start_show(struct device *dev, struct device_attribute *attr,
 {
        struct cxl_decoder *cxld = to_cxl_decoder(dev);
 
-       return sysfs_emit(buf, "%#llx\n", cxld->range.start);
+       return sysfs_emit(buf, "%#llx\n", cxld->res.start);
 }
 static DEVICE_ATTR_RO(start);
 

drivers/cxl/core/bus.c static ssize_t size_show(struct device *dev, struct device_attribute *attr, · line 59

Quoted 5 lines
drivers/cxl/core/bus.c +1 -1
@@ -56,7 +56,7 @@ static ssize_t size_show(struct device *dev, struct device_attribute *attr,
 {
        struct cxl_decoder *cxld = to_cxl_decoder(dev);
 
-       return sysfs_emit(buf, "%#llx\n", range_len(&cxld->range));
+       return sysfs_emit(buf, "%#llx\n", resource_size(&cxld->res));

It's not clear to me that anything other than root decoders will host a resource tree, every decoder downstream of the root would reference a subset of the root decoder range. Something like:

drivers/cxl/cxl.h +6 -2
index 5e2e93451928..00bf742396e0 100644
@@ -200,7 +200,8 @@ enum cxl_decoder_type {
  * struct cxl_decoder - CXL address range decode configuration
  * @dev: this decoder's device
  * @id: kernel device name id
- * @range: address range considered by this decoder
+ * @res: platform address range hosted by a root decoder
+ * @range: address range programmed in the (non-root) decoder
  * @interleave_ways: number of cxl_dports in this decode
  * @interleave_granularity: data stride per dport
  * @target_type: accelerator vs expander (type2 vs type3) selector
@@ -211,7 +212,10 @@ enum cxl_decoder_type {
 struct cxl_decoder {
        struct device dev;
        int id;
-       struct range range;
+       union {
+               struct resource res;
+               struct range range;
+       };
        int interleave_ways;
        int interleave_granularity;
        enum cxl_decoder_type target_type;

...because regions will __request_region(&cxld->res, ...) from the root decoder, and all the intervening decoders in that stack will be updated to make that mapping happen.

Note, this is just how I had it roughly mapped out in my head, I defer making it a hard recommendation until I get deeper into this set to see if we diverge.

Quoted 28 lines
drivers/cxl/core/bus.c +0 -0
 }
 static DEVICE_ATTR_RO(size);
 
drivers/cxl/cxl.h +2 -3
index 4483e1a39fc3..7f2e2bdc7883 100644
@@ -195,7 +195,7 @@ enum cxl_decoder_type {
  * struct cxl_decoder - CXL address range decode configuration
  * @dev: this decoder's device
  * @id: kernel device name id
- * @range: address range considered by this decoder
+ * @res: address space resources considered by this decoder
  * @interleave_ways: number of cxl_dports in this decode
  * @interleave_granularity: data stride per dport
  * @target_type: accelerator vs expander (type2 vs type3) selector
@@ -206,7 +206,7 @@ enum cxl_decoder_type {
 struct cxl_decoder {
        struct device dev;
        int id;
-       struct range range;
+       struct resource res;
        int interleave_ways;
        int interleave_granularity;
        enum cxl_decoder_type target_type;
--

2.33.1

#14 lore raw

Re: [RFC PATCH v2 05/28] cxl/core: Convert decoder range to resource

Quoted 101 lines · Dan Williams
Quoted 17 lines · Ben Widawsky

Regions will use the resource API in order to help manage allocated space. As regions are children of the decoder, it makes sense that the parent host the main resource to be suballocated by the region.

---
 drivers/cxl/acpi.c     | 12 ++++--------
 drivers/cxl/core/bus.c |  4 ++--
 drivers/cxl/cxl.h      |  4 ++--
 3 files changed, 8 insertions(+), 12 deletions(-)
drivers/cxl/acpi.c +0 -0
index 7d13e7f0aefc..b972abc9f6ef 100644

drivers/cxl/acpi.c static void cxl_add_cfmws_decoders(struct device *dev, · line 131

Quoted 10 lines
drivers/cxl/acpi.c +3 -4
@@ -126,10 +126,9 @@ static void cxl_add_cfmws_decoders(struct device *dev,
 
                cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
                cxld->target_type = CXL_DECODER_EXPANDER;
-               cxld->range = (struct range) {
-                       .start = cfmws->base_hpa,
-                       .end = cfmws->base_hpa + cfmws->window_size - 1,
-               };
+               cxld->res = (struct resource)DEFINE_RES_MEM_NAMED(cfmws->base_hpa,
+                                                                 cfmws->window_size,
+                                                                 "cfmws");

I think this should just be DEFINE_RES_MEM(), and then set the name of it inside cxl_decoder_add() to the dev_name() of the decoder device. That way a dump of the resource tree hierarchy makes sense compared to the device hierarchy with actual device names not a series of repeating "cfmws" entries.

Quoted 29 lines
drivers/cxl/acpi.c +1 -4
                cxld->interleave_ways = CFMWS_INTERLEAVE_WAYS(cfmws);
                cxld->interleave_granularity =
                        CFMWS_INTERLEAVE_GRANULARITY(cfmws);
@@ -339,10 +338,7 @@ static int add_host_bridge_uport(struct device *match, void *arg)
        cxld->interleave_ways = 1;
        cxld->interleave_granularity = PAGE_SIZE;
        cxld->target_type = CXL_DECODER_EXPANDER;
-       cxld->range = (struct range) {
-               .start = 0,
-               .end = -1,
-       };
+       cxld->res = (struct resource)DEFINE_RES_MEM(0, 0);
 
        device_lock(&port->dev);
        dport = list_first_entry(&port->dports, typeof(*dport), list);
drivers/cxl/core/bus.c +1 -1
index ebd061d03950..454d4d846eb2 100644
@@ -47,7 +47,7 @@ static ssize_t start_show(struct device *dev, struct device_attribute *attr,
 {
        struct cxl_decoder *cxld = to_cxl_decoder(dev);
 
-       return sysfs_emit(buf, "%#llx\n", cxld->range.start);
+       return sysfs_emit(buf, "%#llx\n", cxld->res.start);
 }
 static DEVICE_ATTR_RO(start);
 

drivers/cxl/core/bus.c static ssize_t size_show(struct device *dev, struct device_attribute *attr, · line 59

Quoted 5 lines
drivers/cxl/core/bus.c +1 -1
@@ -56,7 +56,7 @@ static ssize_t size_show(struct device *dev, struct device_attribute *attr,
 {
        struct cxl_decoder *cxld = to_cxl_decoder(dev);
 
-       return sysfs_emit(buf, "%#llx\n", range_len(&cxld->range));
+       return sysfs_emit(buf, "%#llx\n", resource_size(&cxld->res));

It's not clear to me that anything other than root decoders will host a resource tree, every decoder downstream of the root would reference a subset of the root decoder range. Something like:

drivers/cxl/cxl.h +6 -2
index 5e2e93451928..00bf742396e0 100644
@@ -200,7 +200,8 @@ enum cxl_decoder_type {
  * struct cxl_decoder - CXL address range decode configuration
  * @dev: this decoder's device
  * @id: kernel device name id
- * @range: address range considered by this decoder
+ * @res: platform address range hosted by a root decoder
+ * @range: address range programmed in the (non-root) decoder
  * @interleave_ways: number of cxl_dports in this decode
  * @interleave_granularity: data stride per dport
  * @target_type: accelerator vs expander (type2 vs type3) selector
@@ -211,7 +212,10 @@ enum cxl_decoder_type {
 struct cxl_decoder {
        struct device dev;
        int id;
-       struct range range;
+       union {
+               struct resource res;
+               struct range range;
+       };
        int interleave_ways;
        int interleave_granularity;
        enum cxl_decoder_type target_type;
Quoted 8 lines

...because regions will __request_region(&cxld->res, ...) from the root decoder, and all the intervening decoders in that stack will be updated to make that mapping happen.

Note, this is just how I had it roughly mapped out in my head, I defer making it a hard recommendation until I get deeper into this set to see if we diverge.

You're correct.

I had changed this already for my v3. I ended up with this:
+ union {
+ struct resource cfmws_res;
+ struct resource *decoder_res;
+ };

Not sure if you prefer one way or another.

Quoted 28 lines
Quoted 28 lines
drivers/cxl/core/bus.c +0 -0
 }
 static DEVICE_ATTR_RO(size);
 
drivers/cxl/cxl.h +2 -3
index 4483e1a39fc3..7f2e2bdc7883 100644
@@ -195,7 +195,7 @@ enum cxl_decoder_type {
  * struct cxl_decoder - CXL address range decode configuration
  * @dev: this decoder's device
  * @id: kernel device name id
- * @range: address range considered by this decoder
+ * @res: address space resources considered by this decoder
  * @interleave_ways: number of cxl_dports in this decode
  * @interleave_granularity: data stride per dport
  * @target_type: accelerator vs expander (type2 vs type3) selector
@@ -206,7 +206,7 @@ enum cxl_decoder_type {
 struct cxl_decoder {
        struct device dev;
        int id;
-       struct range range;
+       struct resource res;
        int interleave_ways;
        int interleave_granularity;
        enum cxl_decoder_type target_type;
--

2.33.1

#15 lore raw

Re: [RFC PATCH v2 05/28] cxl/core: Convert decoder range to resource

On Fri, Oct 29, 2021 at 2:27 PM Ben Widawsky <[email protected]> wrote:
[..]

Review

Quoted 10 lines
Quoted 4 lines

Note, this is just how I had it roughly mapped out in my head, I defer making it a hard recommendation until I get deeper into this set to see if we diverge.

You're correct.

I had changed this already for my v3. I ended up with this:

(quoted diff) +2 -0
+       union {
+               struct resource cfmws_res;

"cfmws" is something that will only appear on ACPI based CXL systems, so if it needs a prefix it should be something that does not preclude CXL on other platform firmware architectures. So, "platform_" or "root_"?

Quoted 1 line

+ struct resource *decoder_res;

So the region device is going to be doing the __request_region() from @cfmws_res, not decoders, so why is this a resource and not a range?

#16 lore raw

Re: [RFC PATCH v2 05/28] cxl/core: Convert decoder range to resource

Quoted 9 lines · Dan Williams

On Fri, Oct 29, 2021 at 2:27 PM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 7 lines
Quoted 4 lines

Note, this is just how I had it roughly mapped out in my head, I defer making it a hard recommendation until I get deeper into this set to see if we diverge.

You're correct.

Quoted 8 lines

Review

Quoted 3 lines

I had changed this already for my v3. I ended up with this:

(quoted diff) +2 -0
+       union {
+               struct resource cfmws_res;

"cfmws" is something that will only appear on ACPI based CXL systems, so if it needs a prefix it should be something that does not preclude CXL on other platform firmware architectures. So, "platform_" or "root_"?

Sure. Platform_ is better.

Quoted 5 lines
Quoted 2 lines

+ struct resource *decoder_res;

So the region device is going to be doing the __request_region() from @cfmws_res, not decoders, so why is this a resource and not a range?

Well, sort of. Most of v3 was to ultimately go with the decoders doing the __request_region, although thinking about this further that will not work since multiple decoders might want the same ranges of the parent resource.

So I guess it has to be region. I can go back to renaming it res in that case. You're correct that the region device should own doing the __request_region.

#17 lore raw

Re: [RFC PATCH v2 05/28] cxl/core: Convert decoder range to resource

Quoted 14 lines · Ben Widawsky
Quoted 13 lines · Dan Williams

On Fri, Oct 29, 2021 at 2:27 PM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 10 lines
Quoted 4 lines

Note, this is just how I had it roughly mapped out in my head, I defer making it a hard recommendation until I get deeper into this set to see if we diverge.

You're correct.

I had changed this already for my v3. I ended up with this:

(quoted diff) +2 -0
+       union {
+               struct resource cfmws_res;
Quoted 7 lines
Quoted 4 lines

"cfmws" is something that will only appear on ACPI based CXL systems, so if it needs a prefix it should be something that does not preclude CXL on other platform firmware architectures. So, "platform_" or "root_"?

Sure. Platform_ is better.

Coming back to this. While "CFMWS" is indeed defined as part of ACPI, I do think it could also be a generic name for what it represents. In other words, I don't see any issue with using CFMWS, but, I'll start using "platform" since it seems it's your preference (and I prefer that to "root").

Quoted 12 lines
Quoted 5 lines
Quoted 2 lines

+ struct resource *decoder_res;

So the region device is going to be doing the __request_region() from @cfmws_res, not decoders, so why is this a resource and not a range?

Well, sort of. Most of v3 was to ultimately go with the decoders doing the __request_region, although thinking about this further that will not work since multiple decoders might want the same ranges of the parent resource.

So I guess it has to be region. I can go back to renaming it res in that case. You're correct that the region device should own doing the __request_region.

#18 lore raw

RFC PATCH v2 6/28 cxl: Introduce endpoint decoders

In reply to Ben Widawsky

Endpoints have decoders too. It is useful to share the same infrastructure from cxl_core. Endpoints do not have dports (downstream targets), only the underlying physical medium. As a result, some special casing is needed.

There is no functional change introduced yet as endpoints don't actually enumerate decoders yet.

---
 drivers/cxl/core/bus.c | 40 ++++++++++++++++++++++++++++++++--------
 drivers/cxl/cxl.h      |  3 ++-
 2 files changed, 34 insertions(+), 9 deletions(-)
drivers/cxl/core/bus.c +32 -8
index 454d4d846eb2..5564a71773e2 100644
@@ -175,6 +175,12 @@ static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
 	NULL,
 };
 
+static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = {
+	&cxl_decoder_base_attribute_group,
+	&cxl_base_attribute_group,
+	NULL,
+};
+
 static void cxl_decoder_release(struct device *dev)
 {
 	struct cxl_decoder *cxld = to_cxl_decoder(dev);
@@ -184,6 +190,12 @@ static void cxl_decoder_release(struct device *dev)
 	kfree(cxld);
 }
 
+static const struct device_type cxl_decoder_endpoint_type = {
+	.name = "cxl_decoder_endpoint",
+	.release = cxl_decoder_release,
+	.groups = cxl_decoder_endpoint_attribute_groups,
+};
+
 static const struct device_type cxl_decoder_switch_type = {
 	.name = "cxl_decoder_switch",
 	.release = cxl_decoder_release,
@@ -196,6 +208,11 @@ static const struct device_type cxl_decoder_root_type = {
 	.groups = cxl_decoder_root_attribute_groups,
 };
 
+static bool is_endpoint_decoder(struct device *dev)
+{
+	return dev->type == &cxl_decoder_endpoint_type;
+}
+
 bool is_root_decoder(struct device *dev)
 {
 	return dev->type == &cxl_decoder_root_type;
@@ -483,7 +500,8 @@ static int decoder_populate_targets(struct cxl_decoder *cxld,
 	return rc;
 }
 
-struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
+struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
+				      unsigned int nr_targets)
 {
 	struct cxl_decoder *cxld, cxld_const_init = {
 		.nr_targets = nr_targets,
@@ -491,7 +509,7 @@ struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
 	struct device *dev;
 	int rc = 0;
 
-	if (nr_targets > CXL_DECODER_MAX_INTERLEAVE || nr_targets < 1)
+	if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
 		return ERR_PTR(-EINVAL);
 
 	cxld = kzalloc(struct_size(cxld, target, nr_targets), GFP_KERNEL);
@@ -510,8 +528,11 @@ struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
 	dev->parent = &port->dev;
 	dev->bus = &cxl_bus_type;
 
+	/* Endpoints don't have a target list */
+	if (nr_targets == 0)
+		dev->type = &cxl_decoder_endpoint_type;
 	/* root ports do not have a cxl_port_type parent */
-	if (port->dev.parent->type == &cxl_port_type)
+	else if (port->dev.parent->type == &cxl_port_type)
 		dev->type = &cxl_decoder_switch_type;
 	else
 		dev->type = &cxl_decoder_root_type;
@@ -538,12 +559,15 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
 	if (cxld->interleave_ways < 1)
 		return -EINVAL;
 
-	port = to_cxl_port(cxld->dev.parent);
-	rc = decoder_populate_targets(cxld, port, target_map);
-	if (rc)
-		return rc;
-
 	dev = &cxld->dev;
+
+	port = to_cxl_port(cxld->dev.parent);
+	if (!is_endpoint_decoder(dev)) {
+		rc = decoder_populate_targets(cxld, port, target_map);
+		if (rc)
+			return rc;
+	}
+
 	rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
 	if (rc)
 		return rc;
drivers/cxl/cxl.h +2 -1
index 7f2e2bdc7883..91b8fd54bc93 100644
@@ -293,7 +293,8 @@ int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
-struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets);
+struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
+				      unsigned int nr_targets);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
Signature · 1 line
2.33.1
#19 lore raw

Re: [RFC PATCH v2 06/28] cxl: Introduce endpoint decoders

Quoted 8 lines · Ben Widawsky

Endpoints have decoders too. It is useful to share the same infrastructure from cxl_core. Endpoints do not have dports (downstream targets), only the underlying physical medium. As a result, some special casing is needed.

There is no functional change introduced yet as endpoints don't actually enumerate decoders yet.

The nr_targets type change either needs to be commented on here, or moved to its own patch.

Otherwise, this looks good.

Quoted 119 lines
---
 drivers/cxl/core/bus.c | 40 ++++++++++++++++++++++++++++++++--------
 drivers/cxl/cxl.h      |  3 ++-
 2 files changed, 34 insertions(+), 9 deletions(-)
drivers/cxl/core/bus.c +32 -8
index 454d4d846eb2..5564a71773e2 100644
@@ -175,6 +175,12 @@ static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
        NULL,
 };
 
+static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = {
+       &cxl_decoder_base_attribute_group,
+       &cxl_base_attribute_group,
+       NULL,
+};
+
 static void cxl_decoder_release(struct device *dev)
 {
        struct cxl_decoder *cxld = to_cxl_decoder(dev);
@@ -184,6 +190,12 @@ static void cxl_decoder_release(struct device *dev)
        kfree(cxld);
 }
 
+static const struct device_type cxl_decoder_endpoint_type = {
+       .name = "cxl_decoder_endpoint",
+       .release = cxl_decoder_release,
+       .groups = cxl_decoder_endpoint_attribute_groups,
+};
+
 static const struct device_type cxl_decoder_switch_type = {
        .name = "cxl_decoder_switch",
        .release = cxl_decoder_release,
@@ -196,6 +208,11 @@ static const struct device_type cxl_decoder_root_type = {
        .groups = cxl_decoder_root_attribute_groups,
 };
 
+static bool is_endpoint_decoder(struct device *dev)
+{
+       return dev->type == &cxl_decoder_endpoint_type;
+}
+
 bool is_root_decoder(struct device *dev)
 {
        return dev->type == &cxl_decoder_root_type;
@@ -483,7 +500,8 @@ static int decoder_populate_targets(struct cxl_decoder *cxld,
        return rc;
 }
 
-struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
+struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
+                                     unsigned int nr_targets)
 {
        struct cxl_decoder *cxld, cxld_const_init = {
                .nr_targets = nr_targets,
@@ -491,7 +509,7 @@ struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
        struct device *dev;
        int rc = 0;
 
-       if (nr_targets > CXL_DECODER_MAX_INTERLEAVE || nr_targets < 1)
+       if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
                return ERR_PTR(-EINVAL);
 
        cxld = kzalloc(struct_size(cxld, target, nr_targets), GFP_KERNEL);
@@ -510,8 +528,11 @@ struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
        dev->parent = &port->dev;
        dev->bus = &cxl_bus_type;
 
+       /* Endpoints don't have a target list */
+       if (nr_targets == 0)
+               dev->type = &cxl_decoder_endpoint_type;
        /* root ports do not have a cxl_port_type parent */
-       if (port->dev.parent->type == &cxl_port_type)
+       else if (port->dev.parent->type == &cxl_port_type)
                dev->type = &cxl_decoder_switch_type;
        else
                dev->type = &cxl_decoder_root_type;
@@ -538,12 +559,15 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
        if (cxld->interleave_ways < 1)
                return -EINVAL;
 
-       port = to_cxl_port(cxld->dev.parent);
-       rc = decoder_populate_targets(cxld, port, target_map);
-       if (rc)
-               return rc;
-
        dev = &cxld->dev;
+
+       port = to_cxl_port(cxld->dev.parent);
+       if (!is_endpoint_decoder(dev)) {
+               rc = decoder_populate_targets(cxld, port, target_map);
+               if (rc)
+                       return rc;
+       }
+
        rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
        if (rc)
                return rc;
drivers/cxl/cxl.h +2 -2
index 7f2e2bdc7883..91b8fd54bc93 100644
@@ -293,7 +293,8 @@ int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
-struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets);
+struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
+                                     unsigned int nr_targets);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
--

2.33.1

#20 lore raw

Re: [RFC PATCH v2 06/28] cxl: Introduce endpoint decoders

Quoted 7 lines · Dan Williams
Quoted 6 lines · Ben Widawsky

Endpoints have decoders too. It is useful to share the same infrastructure from cxl_core. Endpoints do not have dports (downstream targets), only the underlying physical medium. As a result, some special casing is needed.

Quoted 5 lines
Quoted 2 lines

There is no functional change introduced yet as endpoints don't actually enumerate decoders yet.

The nr_targets type change either needs to be commented on here, or moved to its own patch.

Do you have a preference?

Quoted 122 lines

Otherwise, this looks good.

Quoted 119 lines
---
 drivers/cxl/core/bus.c | 40 ++++++++++++++++++++++++++++++++--------
 drivers/cxl/cxl.h      |  3 ++-
 2 files changed, 34 insertions(+), 9 deletions(-)
drivers/cxl/core/bus.c +32 -8
index 454d4d846eb2..5564a71773e2 100644
@@ -175,6 +175,12 @@ static const struct attribute_group *cxl_decoder_switch_attribute_groups[] = {
        NULL,
 };
 
+static const struct attribute_group *cxl_decoder_endpoint_attribute_groups[] = {
+       &cxl_decoder_base_attribute_group,
+       &cxl_base_attribute_group,
+       NULL,
+};
+
 static void cxl_decoder_release(struct device *dev)
 {
        struct cxl_decoder *cxld = to_cxl_decoder(dev);
@@ -184,6 +190,12 @@ static void cxl_decoder_release(struct device *dev)
        kfree(cxld);
 }
 
+static const struct device_type cxl_decoder_endpoint_type = {
+       .name = "cxl_decoder_endpoint",
+       .release = cxl_decoder_release,
+       .groups = cxl_decoder_endpoint_attribute_groups,
+};
+
 static const struct device_type cxl_decoder_switch_type = {
        .name = "cxl_decoder_switch",
        .release = cxl_decoder_release,
@@ -196,6 +208,11 @@ static const struct device_type cxl_decoder_root_type = {
        .groups = cxl_decoder_root_attribute_groups,
 };
 
+static bool is_endpoint_decoder(struct device *dev)
+{
+       return dev->type == &cxl_decoder_endpoint_type;
+}
+
 bool is_root_decoder(struct device *dev)
 {
        return dev->type == &cxl_decoder_root_type;
@@ -483,7 +500,8 @@ static int decoder_populate_targets(struct cxl_decoder *cxld,
        return rc;
 }
 
-struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
+struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
+                                     unsigned int nr_targets)
 {
        struct cxl_decoder *cxld, cxld_const_init = {
                .nr_targets = nr_targets,
@@ -491,7 +509,7 @@ struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
        struct device *dev;
        int rc = 0;
 
-       if (nr_targets > CXL_DECODER_MAX_INTERLEAVE || nr_targets < 1)
+       if (nr_targets > CXL_DECODER_MAX_INTERLEAVE)
                return ERR_PTR(-EINVAL);
 
        cxld = kzalloc(struct_size(cxld, target, nr_targets), GFP_KERNEL);
@@ -510,8 +528,11 @@ struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets)
        dev->parent = &port->dev;
        dev->bus = &cxl_bus_type;
 
+       /* Endpoints don't have a target list */
+       if (nr_targets == 0)
+               dev->type = &cxl_decoder_endpoint_type;
        /* root ports do not have a cxl_port_type parent */
-       if (port->dev.parent->type == &cxl_port_type)
+       else if (port->dev.parent->type == &cxl_port_type)
                dev->type = &cxl_decoder_switch_type;
        else
                dev->type = &cxl_decoder_root_type;
@@ -538,12 +559,15 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
        if (cxld->interleave_ways < 1)
                return -EINVAL;
 
-       port = to_cxl_port(cxld->dev.parent);
-       rc = decoder_populate_targets(cxld, port, target_map);
-       if (rc)
-               return rc;
-
        dev = &cxld->dev;
+
+       port = to_cxl_port(cxld->dev.parent);
+       if (!is_endpoint_decoder(dev)) {
+               rc = decoder_populate_targets(cxld, port, target_map);
+               if (rc)
+                       return rc;
+       }
+
        rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
        if (rc)
                return rc;
drivers/cxl/cxl.h +2 -2
index 7f2e2bdc7883..91b8fd54bc93 100644
@@ -293,7 +293,8 @@ int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
-struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port, int nr_targets);
+struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
+                                     unsigned int nr_targets);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
--

2.33.1

#21 lore raw

Re: [RFC PATCH v2 06/28] cxl: Introduce endpoint decoders

Quoted 9 lines · Ben Widawsky
Quoted 7 lines · Dan Williams
Quoted 6 lines · Ben Widawsky

Endpoints have decoders too. It is useful to share the same infrastructure from cxl_core. Endpoints do not have dports (downstream targets), only the underlying physical medium. As a result, some special casing is needed.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

There is no functional change introduced yet as endpoints don't actually enumerate decoders yet.

The nr_targets type change either needs to be commented on here, or moved to its own patch.

Do you have a preference?

Slight preference for separate patch.

#22 lore raw

RFC PATCH v2 7/28 cxl/core: Move target population locking to caller

In reply to Ben Widawsky

A CXL port driver which would be responsible for adding decoders will want to do so during probe. Unfortunately, the device lock is held during probe. Making this change allows for a new lock-free variant of cxl_decoder_add which will be used by the port driver.

---
 drivers/cxl/core/bus.c | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)
drivers/cxl/core/bus.c +12 -17
index 5564a71773e2..5f2bde47a5c2 100644
@@ -478,25 +478,18 @@ static int decoder_populate_targets(struct cxl_decoder *cxld,
 	if (!target_map)
 		return 0;
 
-	device_lock(&port->dev);
-	if (list_empty(&port->dports)) {
-		rc = -EINVAL;
-		goto out_unlock;
-	}
+	device_lock_assert(&port->dev);
+	if (list_empty(&port->dports))
+		return -EINVAL;
 
 	for (i = 0; i < cxld->nr_targets; i++) {
 		struct cxl_dport *dport = find_dport(port, target_map[i]);
 
-		if (!dport) {
-			rc = -ENXIO;
-			goto out_unlock;
-		}
+		if (!dport)
+			return -ENXIO;
 		cxld->target[i] = dport;
 	}
 
-out_unlock:
-	device_unlock(&port->dev);
-
 	return rc;
 }
 
@@ -548,7 +541,7 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
 {
 	struct cxl_port *port;
 	struct device *dev;
-	int rc;
+	int rc = 0;
 
 	if (WARN_ON_ONCE(!cxld))
 		return -EINVAL;
@@ -562,11 +555,13 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
 	dev = &cxld->dev;
 
 	port = to_cxl_port(cxld->dev.parent);
-	if (!is_endpoint_decoder(dev)) {
+
+	device_lock(&port->dev);
+	if (!is_endpoint_decoder(dev))
 		rc = decoder_populate_targets(cxld, port, target_map);
-		if (rc)
-			return rc;
-	}
+	device_unlock(&port->dev);
+	if (rc)
+		return rc;
 
 	rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
 	if (rc)
Signature · 1 line
2.33.1
#23 lore raw

Re: [RFC PATCH v2 07/28] cxl/core: Move target population locking to caller

Quoted 3 lines · Ben Widawsky

A CXL port driver which would be responsible for adding decoders will want to do so during probe.

"In preparation for a port driver that enumerates a descendant port + decoder hierarchy, arrange for an unlocked version of cxl_decoder_add(). Otherwise a port-driver that adds a child decoder will deadlock on the device_lock() in ->probe()."

...although when I write it that way I would expect to see cxl_decoder_add_locked() in this patch.

The port driver patch is big enough already, so pulling the API addition into this patch would be my preference.

#24 lore raw

RFC PATCH v2 8/28 cxl/port: Introduce a port driver

In reply to Ben Widawsky

The CXL port driver will be responsible for managing the decoder resources contained within the port. It will also provide APIs that other drivers will consume for managing these resources.

Since the port driver is responsible for instantiating new decoders, and it does so during probe(), a new API is needed to add decoders for callers which already hold the device lock of the port.

This patch has no functional change because no driver is registering new ports and the root ports that are already registered should be skipped.

---
 .../driver-api/cxl/memory-devices.rst         |   5 +
 drivers/cxl/Makefile                          |   2 +
 drivers/cxl/acpi.c                            |   1 +
 drivers/cxl/core/bus.c                        |  44 +++-
 drivers/cxl/core/regs.c                       |   6 +-
 drivers/cxl/cxl.h                             |  31 ++-
 drivers/cxl/port.c                            | 242 ++++++++++++++++++
 7 files changed, 315 insertions(+), 16 deletions(-)
 create mode 100644 drivers/cxl/port.c
Documentation/driver-api/cxl/memory-devices.rst +5 -0
index 3b8f41395f6b..fbf0393cdddc 100644
@@ -28,6 +28,11 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/pci.c
    :internal:
 
+CXL Port
+--------
+.. kernel-doc:: drivers/cxl/port.c
+   :doc: cxl port
+
 CXL Core
 --------
 .. kernel-doc:: drivers/cxl/cxl.h
drivers/cxl/Makefile +2 -0
index cf07ae6cea17..40b386aaedf7 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o
 obj-$(CONFIG_CXL_PCI) += cxl_pci.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
@@ -7,3 +8,4 @@ obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 cxl_pci-y := pci.o
 cxl_acpi-y := acpi.o
 cxl_pmem-y := pmem.o
+cxl_port-y := port.o
drivers/cxl/acpi.c +1 -0
index b972abc9f6ef..d61397055e9f 100644
@@ -491,3 +491,4 @@ static struct platform_driver cxl_acpi_driver = {
 module_platform_driver(cxl_acpi_driver);
 MODULE_LICENSE("GPL v2");
 MODULE_IMPORT_NS(CXL);
+MODULE_SOFTDEP("pre: cxl_port");
drivers/cxl/core/bus.c +37 -7

78 lines not on this page · raw message

drivers/cxl/core/regs.c +3 -3
index 40598905c080..c8ab8880b81b 100644
@@ -159,9 +159,8 @@ void cxl_probe_device_regs(struct device *dev, void __iomem *base,
 }
 EXPORT_SYMBOL_GPL(cxl_probe_device_regs);
 
-static void __iomem *devm_cxl_iomap_block(struct device *dev,
-					  resource_size_t addr,
-					  resource_size_t length)
+void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
+				   resource_size_t length)
 {
 	void __iomem *ret_val;
 	struct resource *res;
@@ -180,6 +179,7 @@ static void __iomem *devm_cxl_iomap_block(struct device *dev,
 
 	return ret_val;
 }
+EXPORT_SYMBOL_GPL(devm_cxl_iomap_block);
 
 int cxl_map_component_regs(struct pci_dev *pdev,
 			   struct cxl_component_regs *regs,
drivers/cxl/cxl.h +25 -6
index 91b8fd54bc93..ad22caf9135c 100644
@@ -17,6 +17,9 @@
  * (port-driver, region-driver, nvdimm object-drivers... etc).
  */
 
+/* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */
+#define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K
+
 /* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
 #define CXL_CM_OFFSET 0x1000
 #define CXL_CM_CAP_HDR_OFFSET 0x0
@@ -36,11 +39,22 @@
 #define CXL_HDM_DECODER_CAP_OFFSET 0x0
 #define   CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
-#define CXL_HDM_DECODER0_BASE_LOW_OFFSET 0x10
-#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET 0x14
-#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET 0x18
-#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET 0x1c
-#define CXL_HDM_DECODER0_CTRL_OFFSET 0x20
+#define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
+#define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
+#define CXL_HDM_DECODER_CTRL_OFFSET 0x4
+#define   CXL_HDM_DECODER_ENABLE BIT(1)
+#define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
+#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14)
+#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18)
+#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c)
+#define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20)
+#define   CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0)
+#define   CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
+#define   CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
+#define   CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
+#define   CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
+#define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
+#define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
 
 static inline int cxl_hdm_decoder_count(u32 cap_hdr)
 {
@@ -164,6 +178,8 @@ int cxl_map_device_regs(struct pci_dev *pdev,
 enum cxl_regloc_type;
 int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
 		      struct cxl_register_map *map);
+void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
+				   resource_size_t length);
 
 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
 #define CXL_TARGET_STRLEN 20
@@ -178,7 +194,8 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
 #define CXL_DECODER_F_TYPE2 BIT(2)
 #define CXL_DECODER_F_TYPE3 BIT(3)
 #define CXL_DECODER_F_LOCK  BIT(4)
-#define CXL_DECODER_F_MASK  GENMASK(4, 0)
+#define CXL_DECODER_F_EN    BIT(5)
+#define CXL_DECODER_F_MASK  GENMASK(5, 0)
 
 enum cxl_decoder_type {
        CXL_DECODER_ACCELERATOR = 2,
@@ -295,6 +312,7 @@ struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
 struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
 				      unsigned int nr_targets);
+int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
@@ -323,6 +341,7 @@ void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 
 #define CXL_DEVICE_NVDIMM_BRIDGE	1
 #define CXL_DEVICE_NVDIMM		2
+#define CXL_DEVICE_PORT			3
 
 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
 #define CXL_MODALIAS_FMT "cxl:t%d"
drivers/cxl/port.c new file +242 -0

242 lines not on this page · raw message

Signature · 1 line
2.33.1
#25 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 2 lines · Ben Widawsky

The CXL port driver will be responsible for managing the decoder

Small quibble to use present tense, the future is now with this patch!

Quoted 43 lines

resources contained within the port. It will also provide APIs that other drivers will consume for managing these resources.

Since the port driver is responsible for instantiating new decoders, and it does so during probe(), a new API is needed to add decoders for callers which already hold the device lock of the port.

This patch has no functional change because no driver is registering new ports and the root ports that are already registered should be skipped.

---
 .../driver-api/cxl/memory-devices.rst         |   5 +
 drivers/cxl/Makefile                          |   2 +
 drivers/cxl/acpi.c                            |   1 +
 drivers/cxl/core/bus.c                        |  44 +++-
 drivers/cxl/core/regs.c                       |   6 +-
 drivers/cxl/cxl.h                             |  31 ++-
 drivers/cxl/port.c                            | 242 ++++++++++++++++++
 7 files changed, 315 insertions(+), 16 deletions(-)
 create mode 100644 drivers/cxl/port.c
Documentation/driver-api/cxl/memory-devices.rst +5 -0
index 3b8f41395f6b..fbf0393cdddc 100644
@@ -28,6 +28,11 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/pci.c
    :internal:
 
+CXL Port
+--------
+.. kernel-doc:: drivers/cxl/port.c
+   :doc: cxl port
+
 CXL Core
 --------
 .. kernel-doc:: drivers/cxl/cxl.h
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644

drivers/cxl/Makefile line 3

Quoted 3 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Quoted 13 lines
drivers/cxl/Makefile +1 -0
 obj-$(CONFIG_CXL_PCI) += cxl_pci.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
@@ -7,3 +8,4 @@ obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 cxl_pci-y := pci.o
 cxl_acpi-y := acpi.o
 cxl_pmem-y := pmem.o
+cxl_port-y := port.o
drivers/cxl/acpi.c +0 -0
index b972abc9f6ef..d61397055e9f 100644

drivers/cxl/acpi.c static struct platform_driver cxl_acpi_driver = { · line 494

Quoted 4 lines
drivers/cxl/acpi.c +1 -0
@@ -491,3 +491,4 @@ static struct platform_driver cxl_acpi_driver = {
 module_platform_driver(cxl_acpi_driver);
 MODULE_LICENSE("GPL v2");
 MODULE_IMPORT_NS(CXL);
+MODULE_SOFTDEP("pre: cxl_port");

Why does cxl_acpi depend on cxl_port being loaded at this point? I think this wants to wait for a future patch where cxl_acpi uses port driver services.

Quoted 40 lines
drivers/cxl/core/bus.c +13 -2
index 5f2bde47a5c2..dffbd0ac64af 100644
@@ -266,6 +266,7 @@ struct cxl_port *to_cxl_port(struct device *dev)
                return NULL;
        return container_of(dev, struct cxl_port, dev);
 }
+EXPORT_SYMBOL_GPL(to_cxl_port);
 
 static void unregister_port(void *_port)
 {
@@ -537,11 +538,21 @@ struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
 }
 EXPORT_SYMBOL_GPL(cxl_decoder_alloc);
 
-int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
+/**
+ * cxl_decoder_add_locked() - Adds a decoder with port locked.
+ * @cxld: Decoder to be added
+ * @target_map: Downstream port array of port ids
+ *
+ * The parent port owning the @cxld must have its device mutex locked before
+ * calling this function.
+ *
+ * Returns 0 if successful; otherwise, a negative error code is returned.
+ */
+int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map)
 {
        struct cxl_port *port;
        struct device *dev;
-       int rc = 0;
+       int rc;
 
        if (WARN_ON_ONCE(!cxld))
                return -EINVAL;
@@ -556,12 +567,11 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
 
        port = to_cxl_port(cxld->dev.parent);
 

drivers/cxl/core/bus.c int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map) · line 574

Quoted 10 lines
drivers/cxl/core/bus.c +4 -5
-       device_lock(&port->dev);
-       if (!is_endpoint_decoder(dev))
+       if (!is_endpoint_decoder(dev)) {
                rc = decoder_populate_targets(cxld, port, target_map);
-       device_unlock(&port->dev);
-       if (rc)
-               return rc;
+               if (rc)
+                       return rc;
+       }

Yeah, this hunk is proof that the api change belongs in the previous patch.

Quoted 12 lines
drivers/cxl/core/bus.c +5 -0
        rc = dev_set_name(dev, "decoder%d.%d", port->id, cxld->id);
        if (rc)
@@ -569,6 +579,24 @@ int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
 
        return device_add(dev);
 }
+EXPORT_SYMBOL_GPL(cxl_decoder_add_locked);
+
+int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map)
+{
+       int rc;

drivers/cxl/core/bus.c int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map) · line 596

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+
+       if (WARN_ON_ONCE(!cxld))
+               return -EINVAL;
+
+       if (WARN_ON_ONCE(IS_ERR(cxld)))
+               return PTR_ERR(cxld);
+
+       device_lock(&to_cxl_port(cxld->dev.parent)->dev);
+       rc = cxl_decoder_add_locked(cxld, target_map);
+       device_unlock(&to_cxl_port(cxld->dev.parent)->dev);

I don't like that this converts the parent device twice, especially since to_cxl_port() does extra work for type validation, so please add a local @port variable.

Quoted 32 lines
drivers/cxl/core/bus.c +5 -0
+
+       return rc;
+}
 EXPORT_SYMBOL_GPL(cxl_decoder_add);
 
 static void cxld_unregister(void *dev)
@@ -627,6 +655,8 @@ static int cxl_device_id(struct device *dev)
                return CXL_DEVICE_NVDIMM_BRIDGE;
        if (dev->type == &cxl_nvdimm_type)
                return CXL_DEVICE_NVDIMM;
+       if (dev->type == &cxl_port_type)
+               return CXL_DEVICE_PORT;
        return 0;
 }
 
drivers/cxl/core/regs.c +2 -3
index 40598905c080..c8ab8880b81b 100644
@@ -159,9 +159,8 @@ void cxl_probe_device_regs(struct device *dev, void __iomem *base,
 }
 EXPORT_SYMBOL_GPL(cxl_probe_device_regs);
 
-static void __iomem *devm_cxl_iomap_block(struct device *dev,
-                                         resource_size_t addr,
-                                         resource_size_t length)
+void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
+                                  resource_size_t length)
 {
        void __iomem *ret_val;
        struct resource *res;

drivers/cxl/core/regs.c static void __iomem *devm_cxl_iomap_block(struct device *dev, · line 182

Quoted 4 lines
drivers/cxl/core/regs.c +1 -0
@@ -180,6 +179,7 @@ static void __iomem *devm_cxl_iomap_block(struct device *dev,
 
        return ret_val;
 }
+EXPORT_SYMBOL_GPL(devm_cxl_iomap_block);

I think it's well past time to convert all the EXPORT_SYMBOL_GPL in the core to EXPORT_SYMBOL_NS_GPL($symbol, CXL).

Quoted 55 lines
drivers/cxl/core/regs.c +0 -0
 int cxl_map_component_regs(struct pci_dev *pdev,
                           struct cxl_component_regs *regs,
drivers/cxl/cxl.h +21 -5
index 91b8fd54bc93..ad22caf9135c 100644
@@ -17,6 +17,9 @@
  * (port-driver, region-driver, nvdimm object-drivers... etc).
  */
 
+/* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */
+#define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K
+
 /* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
 #define CXL_CM_OFFSET 0x1000
 #define CXL_CM_CAP_HDR_OFFSET 0x0
@@ -36,11 +39,22 @@
 #define CXL_HDM_DECODER_CAP_OFFSET 0x0
 #define   CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
-#define CXL_HDM_DECODER0_BASE_LOW_OFFSET 0x10
-#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET 0x14
-#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET 0x18
-#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET 0x1c
-#define CXL_HDM_DECODER0_CTRL_OFFSET 0x20
+#define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
+#define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
+#define CXL_HDM_DECODER_CTRL_OFFSET 0x4
+#define   CXL_HDM_DECODER_ENABLE BIT(1)
+#define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
+#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14)
+#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18)
+#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c)
+#define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20)
+#define   CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0)
+#define   CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
+#define   CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
+#define   CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
+#define   CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
+#define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
+#define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
 
 static inline int cxl_hdm_decoder_count(u32 cap_hdr)
 {
@@ -164,6 +178,8 @@ int cxl_map_device_regs(struct pci_dev *pdev,
 enum cxl_regloc_type;
 int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
                      struct cxl_register_map *map);
+void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
+                                  resource_size_t length);
 
 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
 #define CXL_TARGET_STRLEN 20

drivers/cxl/cxl.h int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type, · line 197

Quoted 5 lines
drivers/cxl/cxl.h +1 -1
@@ -178,7 +194,8 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
 #define CXL_DECODER_F_TYPE2 BIT(2)
 #define CXL_DECODER_F_TYPE3 BIT(3)
 #define CXL_DECODER_F_LOCK  BIT(4)
-#define CXL_DECODER_F_MASK  GENMASK(4, 0)
+#define CXL_DECODER_F_EN    BIT(5)

Spell out ENABLE.

Quoted 65 lines
drivers/cxl/cxl.h +3 -0
+#define CXL_DECODER_F_MASK  GENMASK(5, 0)
 
 enum cxl_decoder_type {
        CXL_DECODER_ACCELERATOR = 2,
@@ -295,6 +312,7 @@ struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
 struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
                                      unsigned int nr_targets);
+int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
@@ -323,6 +341,7 @@ void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 
 #define CXL_DEVICE_NVDIMM_BRIDGE       1
 #define CXL_DEVICE_NVDIMM              2
+#define CXL_DEVICE_PORT                        3
 
 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
 #define CXL_MODALIAS_FMT "cxl:t%d"
drivers/cxl/port.c new file +39 -0
new file mode 100644
index 000000000000..ebbfb72ae995
@@ -0,0 +1,242 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2021 Intel Corporation. All rights reserved. */
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include "cxlmem.h"
+
+/**
+ * DOC: cxl port
+ *
+ * The port driver implements the set of functionality needed to allow full
+ * decoder enumeration and routing. A CXL port is an abstraction of a CXL
+ * component that implements some amount of CXL decoding of CXL.mem traffic.
+ * As of the CXL 2.0 spec, this includes:
+ *
+ *     .. list-table:: CXL Components w/ Ports
+ *             :widths: 25 25 50
+ *             :header-rows: 1
+ *
+ *             * - component
+ *               - upstream
+ *               - downstream
+ *             * - Hostbridge
+ *               - ACPI0016
+ *               - root port
+ *             * - Switch
+ *               - Switch Upstream Port
+ *               - Switch Downstream Port
+ *             * - Endpoint (not yet implemented)
+ *               - Endpoint Port
+ *               - N/A
+ *
+ * The primary service this driver provides is enumerating HDM decoders and
+ * presenting APIs to other drivers to utilize the decoders.
+ */
+
+struct cxl_port_data {
+       struct cxl_component_regs regs;

drivers/cxl/port.c line 49

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+
+       struct port_caps {
+               unsigned int count;
+               unsigned int tc;
+               unsigned int interleave11_8;
+               unsigned int interleave14_12;
+       } caps;
+};
+
+static inline int cxl_hdm_decoder_ig(u32 ctrl)

No need for plain inline in C files.

It's not clear why this simple helper needs a "cxl_hdm_decoder" namespace prefix?

drivers/cxl/port.c line 54

Quoted 5 lines
drivers/cxl/port.c new file +5 -0
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl);
+
+       return 8 + val;
+}

Why is this return a power of 2 value...

drivers/cxl/port.c line 60

Quoted 6 lines
drivers/cxl/port.c new file +6 -0
+
+static inline int cxl_hdm_decoder_iw(u32 ctrl)
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl);
+
+       return 1 << val;

...while this one is converted to absolute values.

These could just be:

unsigned int to_interleave_granularity(u32 ctrl)
unsigned int to_interleave_ways(u32 ctrl)

...and return units in bytes.

Quoted 19 lines
drivers/cxl/port.c new file +19 -0
+}
+
+static void get_caps(struct cxl_port *port, struct cxl_port_data *cpd)
+{
+       void __iomem *hdm_decoder = cpd->regs.hdm_decoder;
+       struct port_caps *caps = &cpd->caps;
+       u32 hdm_cap;
+
+       hdm_cap = readl(hdm_decoder + CXL_HDM_DECODER_CAP_OFFSET);
+
+       caps->count = cxl_hdm_decoder_count(hdm_cap);
+       caps->tc = FIELD_GET(CXL_HDM_DECODER_TARGET_COUNT_MASK, hdm_cap);
+       caps->interleave11_8 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_11_8, hdm_cap);
+       caps->interleave14_12 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap);
+}
+
+static int map_regs(struct cxl_port *port, void __iomem *crb,

drivers/cxl/port.c line 89

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+                   struct cxl_port_data *cpd)
+{
+       struct cxl_register_map map;
+       struct cxl_component_reg_map *comp_map = &map.component_map;
+
+       cxl_probe_component_regs(&port->dev, crb, comp_map);
+       if (!comp_map->hdm_decoder.valid) {
+               dev_err(&port->dev, "HDM decoder registers invalid\n");
+               return -ENXIO;
+       }

Perhaps promote cxl_probe_regs() from the cxl_pci to the core and make it take a dev instead of a pdev, then you can do:

cxl_probe_regs(&port_dev->dev, CXL_REGLOC_RBI_COMPONENT)

...instead of open coding it again?

Quoted 14 lines
drivers/cxl/port.c new file +14 -0
+
+       cpd->regs.hdm_decoder = crb + comp_map->hdm_decoder.offset;
+
+       return 0;
+}
+
+static u64 get_decoder_size(void __iomem *hdm_decoder, int n)
+{
+       u32 ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(n));
+
+       if (!!FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+               return 0;
+
+       return ioread64_hi_lo(hdm_decoder +

drivers/cxl/port.c line 113

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+                             CXL_HDM_DECODER0_SIZE_LOW_OFFSET(n));
+}
+
+static bool is_endpoint_port(struct cxl_port *port)
+{
+       if (!port->uport->driver)
+               return false;
+
+       return to_cxl_drv(port->uport->driver)->id ==
+              CXL_DEVICE_MEMORY_EXPANDER;

Why does endpoint port device type determination need to reach through and read the driver type?

Quoted 38 lines
drivers/cxl/port.c new file +38 -0
+}
+
+static int enumerate_hdm_decoders(struct cxl_port *port,
+                                 struct cxl_port_data *portdata)
+{
+       int i = 0;
+
+       for (i = 0; i < portdata->caps.count; i++) {
+               int iw = 1, ig = 0, rc, target_count = portdata->caps.tc;
+               void __iomem *hdm_decoder = portdata->regs.hdm_decoder;
+               enum cxl_decoder_type type = CXL_DECODER_EXPANDER;
+               struct resource res = DEFINE_RES_MEM(0, 0);
+               struct cxl_decoder *cxld;
+               int *target_map = NULL;
+               u64 size;
+
+               if (is_endpoint_port(port))
+                       target_count = 0;
+
+               cxld = cxl_decoder_alloc(port, target_count);
+               if (IS_ERR(cxld)) {
+                       dev_warn(&port->dev,
+                                "Failed to allocate the decoder\n");
+                       return PTR_ERR(cxld);
+               }
+
+               size = get_decoder_size(hdm_decoder, i);
+               if (size != 0) {
+                       int temp[CXL_DECODER_MAX_INTERLEAVE];
+                       u64 target_list, base;
+                       u32 ctrl;
+                       int j;
+
+                       target_map = temp;
+                       ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(i));
+                       base = ioread64_hi_lo(hdm_decoder + CXL_HDM_DECODER0_BASE_LOW_OFFSET(i));
+                       res = (struct resource)DEFINE_RES_MEM(base, size);
+

drivers/cxl/port.c line 161

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+                       cxld->flags = CXL_DECODER_F_EN;
+                       iw = cxl_hdm_decoder_iw(ctrl);
+                       ig = cxl_hdm_decoder_ig(ctrl);
+
+                       if (FIELD_GET(CXL_HDM_DECODER0_CTRL_TYPE, ctrl) == 0)
+                               type = CXL_DECODER_ACCELERATOR;
+
+                       target_list = ioread64_hi_lo(hdm_decoder + CXL_HDM_DECODER0_TL_LOW(i));
+                       for (j = 0; j < iw; j++)
+                               target_map[j] = (target_list >> (j * 8)) & 0xff;

Perhaps this can be cleaned up with:

union {
    u64 value;
    char target_id[8];
} target_list;

target_list.value = ioread64_hi_lo(hdm_decoder + CXL_HDM_DECODER0_TL_LOW(i)); for (j = 0; j < interleave_ways; j++)

    target_map[j] = target_list.target_id[j];

drivers/cxl/port.c line 173

Quoted 12 lines
drivers/cxl/port.c new file +12 -0
+               }
+
+               cxld->target_type = type;
+               cxld->res = res;
+               cxld->interleave_ways = iw;
+               cxld->interleave_granularity = ig;
+
+               rc = cxl_decoder_add_locked(cxld, target_map);
+               if (rc)
+                       put_device(&cxld->dev);
+               else
+                       rc = cxl_decoder_autoremove(port->uport->parent, cxld);

This looks broken to me.

With the assumption that the CXL subsystem wants the property that the entire port object hierarchy is removed when cxl_acpi is removed, and for a subset of the hierarchy to be removed when an individual port is disabled (detached from the cxl_port driver) then I believe we want something like the patch at the bottom of this mail... only compile tested:

Quoted 15 lines
drivers/cxl/port.c new file +15 -0
+               if (rc)
+                       dev_err(&port->dev, "Failed to add decoder\n");
+       }
+
+       return 0;
+}
+
+static int cxl_port_probe(struct device *dev)
+{
+       struct cxl_port *port = to_cxl_port(dev);
+       struct cxl_port_data *portdata;
+       void __iomem *crb;
+       u32 ctrl;
+       int rc;
+

drivers/cxl/port.c line 198

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+       if (port->component_reg_phys == CXL_RESOURCE_NONE)
+               return 0;
+
+       portdata = devm_kzalloc(dev, sizeof(*portdata), GFP_KERNEL);
+       if (!portdata)
+               return -ENOMEM;
+
+       crb = devm_cxl_iomap_block(&port->dev, port->component_reg_phys,
+                                  CXL_COMPONENT_REG_BLOCK_SIZE);
+       if (IS_ERR_OR_NULL(crb)) {

devm_cxl_iomap_block() does not return an ERR_PTR so this should just be a plain NULL check.

Quoted 9 lines
drivers/cxl/port.c new file +9 -0
+               dev_err(&port->dev, "No component registers mapped\n");
+               return -ENXIO;
+       }
+
+       rc = map_regs(port, crb, portdata);
+       if (rc)
+               return rc;
+
+       get_caps(port, portdata);

drivers/cxl/port.c line 217

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+       if (portdata->caps.count == 0) {
+               dev_err(&port->dev, "Spec violation. Caps invalid\n");
+               return -ENXIO;
+       }
+
+       /*
+        * Enable HDM decoders for this port.
+        *
+        * FIXME: If the component was using DVSEC range registers for decode,
+        * this will destroy that.

Yeah, definitely need to check that before this patch can move forward. Perhaps a port should not even be registered if DVSEC Memory_Size && Mem_Enable are non zero, that device is explicitly opting out of being a part of the CXL 2.0 subsystem hierarchy. However, we might still need to track it and potentially reserve it out of CFMWS capacity to make sure nothing else collides with it. I'll also note that "ECN: Devices operating in CXL 1.1 mode with no RCRB" was recently published that reads on what the driver should do here.

drivers/cxl/port.c line 221

Quoted 4 lines
drivers/cxl/port.c new file +4 -0
+        */
+       ctrl = readl(portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
+       ctrl |= CXL_HDM_DECODER_ENABLE;
+       writel(ctrl, portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);

I feel like that if the driver finds it enabled it should leave it enabled at ->remove() time, as you have it here, as BIOS might not be expecting the OS to disable a decoder it set up. However, if the driver actually does the enable, then it should pair it with a disable at the end of time, so not a blind enable, but one that conditionally arranges for the unwind.

Quoted 22 lines
drivers/cxl/port.c new file +21 -1
+
+       rc = enumerate_hdm_decoders(port, portdata);
+       if (rc) {
+               dev_err(&port->dev, "Couldn't enumerate decoders (%d)\n", rc);
+               return rc;
+       }
+
+       dev_set_drvdata(dev, portdata);
+       return 0;
+}
+
+static struct cxl_driver cxl_port_driver = {
+       .name = "cxl_port",
+       .probe = cxl_port_probe,
+       .id = CXL_DEVICE_PORT,
+};
+module_cxl_driver(cxl_port_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(CXL);
+MODULE_ALIAS_CXL(CXL_DEVICE_PORT);
--
Quoted 1 line

2.33.1

Proposed infrastructure for a new cxl_port_decoder_autoremove():

drivers/cxl/acpi.c +0 -0
index 3163167ecc3a..78f8313a1069 100644
@@ -374,6 +374,11 @@ static int add_root_nvdimm_bridge(struct device

*match, void *data)

        return 1;

}

+static void clear_cxl_topology_host(void *data) +{
+ set_cxl_topology_host(NULL);
+}
+
static int cxl_acpi_probe(struct platform_device *pdev)

 {
        int rc;
@@ -382,6 +387,11 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        struct acpi_device *adev = ACPI_COMPANION(host);
        struct cxl_cfmws_context ctx;
+       set_cxl_topology_host(host);
+       rc = devm_add_action_or_reset(host, clear_cxl_topology_host, host);
+       if (rc)
+               return rc;
+
        root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
        if (IS_ERR(root_port))
                return PTR_ERR(root_port);
drivers/cxl/core/bus.c +1 -0
index 17a4fff029f8..3146b6aa0a2f 100644
@@ -3,6 +3,7 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/device.h>
 #include <linux/module.h>
+#include <linux/rwsem.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/idr.h>
@@ -563,6 +564,84 @@ int cxl_decoder_autoremove(struct device *host,

struct cxl_decoder *cxld)
} EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);

+void trigger_decoder_autoremove(void *data)
+{
+ struct cxl_decoder *cxld = data;
+ struct device *host = get_cxl_topology_host();
+
+ /* The topology host driver beat us to the punch */
+ if (!host)
+ return;
+
+ devm_remove_action(host, cxld_unregister, &cxld->dev);
+ put_cxl_topology_host(host);
+}
+
+/**
+ * cxl_port_decoder_autoremove - remove decoders discovered by the port driver
+ * @cxld: decoder to remove
+ *
+ * CXL.mem requires an intact port / decoder topology from root level
+ * platform decoders to endpoint decoders. Arrange for decoders
+ * enumerated by the port driver to be removed when either the root is
+ * removed (in which the entire hierarchy is removed), or when an
+ * individual port is disabled, in which case only that port's
+ * sub-section of the hierarchy is removed.
+ */
+int cxl_port_decoder_autoremove(struct cxl_decoder *cxld)
+{

+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       struct device *host = get_cxl_topology_host();
+       int rc;

+
+ if (!host)
+ return -ENODEV;
+
+ if (!port->dev.driver) {
+ rc = -ENXIO;
+ goto out;
+ }
+
+ rc = cxl_decoder_autoremove(host, cxld);
+ if (rc)
+ goto out;
+
+ rc = devm_add_action_or_reset(&port->dev, trigger_decoder_autoremove,
+ cxld);
+out:
+ put_cxl_topology_host(host);
+ return rc;
+} +EXPORT_SYMBOL_NS_GPL(cxl_port_decoder_autoremove, CXL); +
+static DECLARE_RWSEM(cxl_topology_rwsem); +static struct device *cxl_topology_host;
+
+void set_cxl_topology_host(struct device *dev) +{

+       down_write(&cxl_topology_rwsem);
+       cxl_topology_host = dev;
+       up_write(&cxl_topology_rwsem);

+}
+EXPORT_SYMBOL_NS_GPL(set_cxl_topology_host, CXL); +
+struct device *get_cxl_topology_host(void)
+{
+ down_read(&cxl_topology_rwsem);
+ if (cxl_topology_host)
+ return cxl_topology_host;
+ up_read(&cxl_topology_rwsem);
+ return NULL;
+}
+EXPORT_SYMBOL_NS_GPL(get_cxl_topology_host, CXL); +
+void put_cxl_topology_host(struct device *dev)
+{
+ WARN_ON(dev != cxl_topology_host);
+ up_read(&cxl_topology_rwsem);
+}
+EXPORT_SYMBOL_NS_GPL(put_cxl_topology_host, CXL); +
/**
* __cxl_driver_register - register a driver for the cxl bus
* @cxl_drv: cxl driver structure to attach

drivers/cxl/cxl.h +0 -0
index 5e2e93451928..bd7b008207a4 100644
@@ -302,6 +302,10 @@ struct cxl_decoder *cxl_decoder_alloc(struct

cxl_port *port, int nr_targets);
int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map); int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);

+void set_cxl_topology_host(struct device *dev);
+struct device *get_cxl_topology_host(void);
+void put_cxl_topology_host(struct device *dev);
+
extern struct bus_type cxl_bus_type;

struct cxl_driver {

#26 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

On Fri, Oct 29, 2021 at 6:37 PM Dan Williams <[email protected]> wrote:
[..]

Quoted 47 lines

Proposed infrastructure for a new cxl_port_decoder_autoremove():

drivers/cxl/acpi.c +5 -0
index 3163167ecc3a..78f8313a1069 100644
@@ -374,6 +374,11 @@ static int add_root_nvdimm_bridge(struct device
 *match, void *data)
        return 1;
 }
 
+static void clear_cxl_topology_host(void *data)
+{
+       set_cxl_topology_host(NULL);
+}
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
drivers/cxl/acpi.c +5 -0
@@ -382,6 +387,11 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        struct acpi_device *adev = ACPI_COMPANION(host);
        struct cxl_cfmws_context ctx;
 
+       set_cxl_topology_host(host);
+       rc = devm_add_action_or_reset(host, clear_cxl_topology_host, host);
+       if (rc)
+               return rc;
+
        root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
        if (IS_ERR(root_port))
                return PTR_ERR(root_port);
drivers/cxl/core/bus.c +1 -0
index 17a4fff029f8..3146b6aa0a2f 100644
@@ -3,6 +3,7 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/device.h>
 #include <linux/module.h>
+#include <linux/rwsem.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/idr.h>
@@ -563,6 +564,84 @@ int cxl_decoder_autoremove(struct device *host,
 struct cxl_decoder *cxld)
 }

drivers/cxl/core/bus.c int cxl_decoder_autoremove(struct device *host, · line 575

Quoted 10 lines
drivers/cxl/core/bus.c +8 -0
 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
 
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;

This can be dropped or turned into a WARN_ON() because something is wrong if the port removal is being triggered after the topology host has already went away.

drivers/cxl/core/bus.c int cxl_decoder_autoremove(struct device *host, · line 577

Quoted 2 lines
drivers/cxl/core/bus.c +2 -0
+
+       devm_remove_action(host, cxld_unregister, &cxld->dev);

Should be devm_release_action().

#27 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 52 lines · Dan Williams

On Fri, Oct 29, 2021 at 6:37 PM Dan Williams <[email protected]> wrote:
[..]

Quoted 49 lines

Proposed infrastructure for a new cxl_port_decoder_autoremove():

drivers/cxl/acpi.c +5 -0
index 3163167ecc3a..78f8313a1069 100644
@@ -374,6 +374,11 @@ static int add_root_nvdimm_bridge(struct device
 *match, void *data)
        return 1;
 }
 
+static void clear_cxl_topology_host(void *data)
+{
+       set_cxl_topology_host(NULL);
+}
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
drivers/cxl/acpi.c +5 -0
@@ -382,6 +387,11 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        struct acpi_device *adev = ACPI_COMPANION(host);
        struct cxl_cfmws_context ctx;
 
+       set_cxl_topology_host(host);
+       rc = devm_add_action_or_reset(host, clear_cxl_topology_host, host);
+       if (rc)
+               return rc;
+
        root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
        if (IS_ERR(root_port))
                return PTR_ERR(root_port);
drivers/cxl/core/bus.c +1 -0
index 17a4fff029f8..3146b6aa0a2f 100644
@@ -3,6 +3,7 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/device.h>
 #include <linux/module.h>
+#include <linux/rwsem.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/idr.h>
@@ -563,6 +564,84 @@ int cxl_decoder_autoremove(struct device *host,
 struct cxl_decoder *cxld)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
 
Quoted 12 lines

drivers/cxl/core/bus.c int cxl_decoder_autoremove(struct device *host, · line 575

Quoted 8 lines
drivers/cxl/core/bus.c +8 -0
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;

This can be dropped or turned into a WARN_ON() because something is wrong if the port removal is being triggered after the topology host has already went away.

It further occurs to me that if all ports are guaranteed to be torn down by topology host removal, then we might not even need to play these games at all. cxl_acpi is already unregistering all of its child ports, we just need the property that if a port adds a child port it remove it. Then I don't think we even need cxl_topology_host games.

#28 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 66 lines · Dan Williams
Quoted 52 lines · Dan Williams

On Fri, Oct 29, 2021 at 6:37 PM Dan Williams <[email protected]> wrote:
[..]

Quoted 49 lines

Proposed infrastructure for a new cxl_port_decoder_autoremove():

drivers/cxl/acpi.c +5 -0
index 3163167ecc3a..78f8313a1069 100644
@@ -374,6 +374,11 @@ static int add_root_nvdimm_bridge(struct device
 *match, void *data)
        return 1;
 }
 
+static void clear_cxl_topology_host(void *data)
+{
+       set_cxl_topology_host(NULL);
+}
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
drivers/cxl/acpi.c +5 -0
@@ -382,6 +387,11 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        struct acpi_device *adev = ACPI_COMPANION(host);
        struct cxl_cfmws_context ctx;
 
+       set_cxl_topology_host(host);
+       rc = devm_add_action_or_reset(host, clear_cxl_topology_host, host);
+       if (rc)
+               return rc;
+
        root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
        if (IS_ERR(root_port))
                return PTR_ERR(root_port);
drivers/cxl/core/bus.c +1 -0
index 17a4fff029f8..3146b6aa0a2f 100644
@@ -3,6 +3,7 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/device.h>
 #include <linux/module.h>
+#include <linux/rwsem.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/idr.h>
@@ -563,6 +564,84 @@ int cxl_decoder_autoremove(struct device *host,
 struct cxl_decoder *cxld)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
 
Quoted 12 lines

drivers/cxl/core/bus.c int cxl_decoder_autoremove(struct device *host, · line 575

Quoted 8 lines
drivers/cxl/core/bus.c +8 -0
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;

This can be dropped or turned into a WARN_ON() because something is wrong if the port removal is being triggered after the topology host has already went away.

Quoted 5 lines

It further occurs to me that if all ports are guaranteed to be torn down by topology host removal, then we might not even need to play these games at all. cxl_acpi is already unregistering all of its child ports, we just need the property that if a port adds a child port it remove it. Then I don't think we even need cxl_topology_host games.

I think having a handle to the "topology_host" is still valuable since I used it to do the CFMWS lookup. Perhaps you have a different solution for that and I'll see it later.

Is the suggestion here to have unregister_port() be responsible for doing an unregister_dev for all children that are ports, or do you mean something else?

#29 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

In reply to Dan Williams
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644
Quoted 9 lines

drivers/cxl/Makefile line 3

Quoted 4 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

Quoted 13 lines
Quoted 13 lines
drivers/cxl/Makefile +1 -0
 obj-$(CONFIG_CXL_PCI) += cxl_pci.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
@@ -7,3 +8,4 @@ obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 cxl_pci-y := pci.o
 cxl_acpi-y := acpi.o
 cxl_pmem-y := pmem.o
+cxl_port-y := port.o
drivers/cxl/acpi.c +0 -0
index b972abc9f6ef..d61397055e9f 100644
@@ -491,3 +491,4 @@ static struct platform_driver cxl_acpi_driver = {
Quoted 8 lines

drivers/cxl/acpi.c static struct platform_driver cxl_acpi_driver = { · line 494

Quoted 4 lines
drivers/cxl/acpi.c +1 -0
 module_platform_driver(cxl_acpi_driver);
 MODULE_LICENSE("GPL v2");
 MODULE_IMPORT_NS(CXL);
+MODULE_SOFTDEP("pre: cxl_port");

Why does cxl_acpi depend on cxl_port being loaded at this point? I think this wants to wait for a future patch where cxl_acpi uses port driver services.

As of this patch it does use port driver services. cxl/acpi: Map single port host bridge component registers made that the case.

[snip]

Quoted 58 lines
Quoted 45 lines
drivers/cxl/port.c new file +39 -0
new file mode 100644
index 000000000000..ebbfb72ae995
@@ -0,0 +1,242 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2021 Intel Corporation. All rights reserved. */
+#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include "cxlmem.h"
+
+/**
+ * DOC: cxl port
+ *
+ * The port driver implements the set of functionality needed to allow full
+ * decoder enumeration and routing. A CXL port is an abstraction of a CXL
+ * component that implements some amount of CXL decoding of CXL.mem traffic.
+ * As of the CXL 2.0 spec, this includes:
+ *
+ *     .. list-table:: CXL Components w/ Ports
+ *             :widths: 25 25 50
+ *             :header-rows: 1
+ *
+ *             * - component
+ *               - upstream
+ *               - downstream
+ *             * - Hostbridge
+ *               - ACPI0016
+ *               - root port
+ *             * - Switch
+ *               - Switch Upstream Port
+ *               - Switch Downstream Port
+ *             * - Endpoint (not yet implemented)
+ *               - Endpoint Port
+ *               - N/A
+ *
+ * The primary service this driver provides is enumerating HDM decoders and
+ * presenting APIs to other drivers to utilize the decoders.
+ */
+
+struct cxl_port_data {
+       struct cxl_component_regs regs;

drivers/cxl/port.c line 49

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+
+       struct port_caps {
+               unsigned int count;
+               unsigned int tc;
+               unsigned int interleave11_8;
+               unsigned int interleave14_12;
+       } caps;
+};
+
+static inline int cxl_hdm_decoder_ig(u32 ctrl)

No need for plain inline in C files.

Quoted 2 lines

It's not clear why this simple helper needs a "cxl_hdm_decoder" namespace prefix?

I had a patch to share this with acpi driver at one point, but I dropped it. Do you care if I merge those two decoders, or just rename?

Quoted 8 lines

drivers/cxl/port.c line 54

Quoted 6 lines
drivers/cxl/port.c new file +5 -0
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl);
+
+       return 8 + val;
+}

Why is this return a power of 2 value...

I don't understand this comment.

Quoted 73 lines

drivers/cxl/port.c line 60

Quoted 7 lines
drivers/cxl/port.c new file +6 -0
+
+static inline int cxl_hdm_decoder_iw(u32 ctrl)
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl);
+
+       return 1 << val;

...while this one is converted to absolute values.

These could just be:

unsigned int to_interleave_granularity(u32 ctrl)
unsigned int to_interleave_ways(u32 ctrl)

...and return units in bytes.

Quoted 19 lines
drivers/cxl/port.c new file +19 -0
+}
+
+static void get_caps(struct cxl_port *port, struct cxl_port_data *cpd)
+{
+       void __iomem *hdm_decoder = cpd->regs.hdm_decoder;
+       struct port_caps *caps = &cpd->caps;
+       u32 hdm_cap;
+
+       hdm_cap = readl(hdm_decoder + CXL_HDM_DECODER_CAP_OFFSET);
+
+       caps->count = cxl_hdm_decoder_count(hdm_cap);
+       caps->tc = FIELD_GET(CXL_HDM_DECODER_TARGET_COUNT_MASK, hdm_cap);
+       caps->interleave11_8 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_11_8, hdm_cap);
+       caps->interleave14_12 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap);
+}
+
+static int map_regs(struct cxl_port *port, void __iomem *crb,

drivers/cxl/port.c line 89

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+                   struct cxl_port_data *cpd)
+{
+       struct cxl_register_map map;
+       struct cxl_component_reg_map *comp_map = &map.component_map;
+
+       cxl_probe_component_regs(&port->dev, crb, comp_map);
+       if (!comp_map->hdm_decoder.valid) {
+               dev_err(&port->dev, "HDM decoder registers invalid\n");
+               return -ENXIO;
+       }

Perhaps promote cxl_probe_regs() from the cxl_pci to the core and make it take a dev instead of a pdev, then you can do:

cxl_probe_regs(&port_dev->dev, CXL_REGLOC_RBI_COMPONENT)

...instead of open coding it again?

Quoted 19 lines
drivers/cxl/port.c new file +19 -0
+
+       cpd->regs.hdm_decoder = crb + comp_map->hdm_decoder.offset;
+
+       return 0;
+}
+
+static u64 get_decoder_size(void __iomem *hdm_decoder, int n)
+{
+       u32 ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(n));
+
+       if (!!FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+               return 0;
+
+       return ioread64_hi_lo(hdm_decoder +
+                             CXL_HDM_DECODER0_SIZE_LOW_OFFSET(n));
+}
+
+static bool is_endpoint_port(struct cxl_port *port)
+{
Quoted 9 lines

drivers/cxl/port.c line 113

Quoted 5 lines
drivers/cxl/port.c new file +5 -0
+       if (!port->uport->driver)
+               return false;
+
+       return to_cxl_drv(port->uport->driver)->id ==
+              CXL_DEVICE_MEMORY_EXPANDER;

Why does endpoint port device type determination need to reach through and read the driver type?

I couldn't figure out a better way at this point in enumeration. I'm open to suggestions.

[snip]

Quoted 7 lines

drivers/cxl/port.c line 119

Quoted 6 lines
drivers/cxl/port.c new file +6 -0
+
+       /*
+        * Enable HDM decoders for this port.
+        *
+        * FIXME: If the component was using DVSEC range registers for decode,
+        * this will destroy that.
Quoted 9 lines

Yeah, definitely need to check that before this patch can move forward. Perhaps a port should not even be registered if DVSEC Memory_Size && Mem_Enable are non zero, that device is explicitly opting out of being a part of the CXL 2.0 subsystem hierarchy. However, we might still need to track it and potentially reserve it out of CFMWS capacity to make sure nothing else collides with it. I'll also note that "ECN: Devices operating in CXL 1.1 mode with no RCRB" was recently published that reads on what the driver should do here.

I believe we want to create the port since we might decide to reset and want control back over it and as you said, safety check other things. The reason it was left as a FIXME is because this belongs in the PCI driver which I didn't really want to touch at this time. I will go back and add that for the next version.

Quoted 11 lines

drivers/cxl/port.c line 123

Quoted 4 lines
drivers/cxl/port.c new file +4 -0
+        */
+       ctrl = readl(portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
+       ctrl |= CXL_HDM_DECODER_ENABLE;
+       writel(ctrl, portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);

I feel like that if the driver finds it enabled it should leave it enabled at ->remove() time, as you have it here, as BIOS might not be expecting the OS to disable a decoder it set up. However, if the driver actually does the enable, then it should pair it with a disable at the end of time, so not a blind enable, but one that conditionally arranges for the unwind.

My thought was that once we enumerate a port, all of it's architectural state belongs to the OS. For us that means blanket enable/disable. I don't feel strongly about this.

Quoted 169 lines
Quoted 23 lines
drivers/cxl/port.c new file +21 -1
+
+       rc = enumerate_hdm_decoders(port, portdata);
+       if (rc) {
+               dev_err(&port->dev, "Couldn't enumerate decoders (%d)\n", rc);
+               return rc;
+       }
+
+       dev_set_drvdata(dev, portdata);
+       return 0;
+}
+
+static struct cxl_driver cxl_port_driver = {
+       .name = "cxl_port",
+       .probe = cxl_port_probe,
+       .id = CXL_DEVICE_PORT,
+};
+module_cxl_driver(cxl_port_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(CXL);
+MODULE_ALIAS_CXL(CXL_DEVICE_PORT);
--
Quoted 1 line

2.33.1

Proposed infrastructure for a new cxl_port_decoder_autoremove():

drivers/cxl/acpi.c +5 -0
index 3163167ecc3a..78f8313a1069 100644
@@ -374,6 +374,11 @@ static int add_root_nvdimm_bridge(struct device
 *match, void *data)
        return 1;
 }
 
+static void clear_cxl_topology_host(void *data)
+{
+       set_cxl_topology_host(NULL);
+}
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
drivers/cxl/acpi.c +5 -0
@@ -382,6 +387,11 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        struct acpi_device *adev = ACPI_COMPANION(host);
        struct cxl_cfmws_context ctx;
 
+       set_cxl_topology_host(host);
+       rc = devm_add_action_or_reset(host, clear_cxl_topology_host, host);
+       if (rc)
+               return rc;
+
        root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
        if (IS_ERR(root_port))
                return PTR_ERR(root_port);
drivers/cxl/core/bus.c +79 -0
index 17a4fff029f8..3146b6aa0a2f 100644
@@ -3,6 +3,7 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/device.h>
 #include <linux/module.h>
+#include <linux/rwsem.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/idr.h>
@@ -563,6 +564,84 @@ int cxl_decoder_autoremove(struct device *host,
 struct cxl_decoder *cxld)
 }
 EXPORT_SYMBOL_NS_GPL(cxl_decoder_autoremove, CXL);
 
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;
+
+       devm_remove_action(host, cxld_unregister, &cxld->dev);
+       put_cxl_topology_host(host);
+}
+
+/**
+ * cxl_port_decoder_autoremove - remove decoders discovered by the port driver
+ * @cxld: decoder to remove
+ *
+ * CXL.mem requires an intact port / decoder topology from root level
+ * platform decoders to endpoint decoders. Arrange for decoders
+ * enumerated by the port driver to be removed when either the root is
+ * removed (in which the entire hierarchy is removed), or when an
+ * individual port is disabled, in which case only that port's
+ * sub-section of the hierarchy is removed.
+ */
+int cxl_port_decoder_autoremove(struct cxl_decoder *cxld)
+{
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       struct device *host = get_cxl_topology_host();
+       int rc;
+
+       if (!host)
+               return -ENODEV;
+
+       if (!port->dev.driver) {
+               rc = -ENXIO;
+               goto out;
+       }
+
+       rc = cxl_decoder_autoremove(host, cxld);
+       if (rc)
+               goto out;
+
+       rc = devm_add_action_or_reset(&port->dev, trigger_decoder_autoremove,
+                                       cxld);
+out:
+       put_cxl_topology_host(host);
+       return rc;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_port_decoder_autoremove, CXL);
+
+static DECLARE_RWSEM(cxl_topology_rwsem);
+static struct device *cxl_topology_host;
+
+void set_cxl_topology_host(struct device *dev)
+{
+       down_write(&cxl_topology_rwsem);
+       cxl_topology_host = dev;
+       up_write(&cxl_topology_rwsem);
+}
+EXPORT_SYMBOL_NS_GPL(set_cxl_topology_host, CXL);
+
+struct device *get_cxl_topology_host(void)
+{
+       down_read(&cxl_topology_rwsem);
+       if (cxl_topology_host)
+               return cxl_topology_host;
+       up_read(&cxl_topology_rwsem);
+       return NULL;
+}
+EXPORT_SYMBOL_NS_GPL(get_cxl_topology_host, CXL);
+
+void put_cxl_topology_host(struct device *dev)
+{
+       WARN_ON(dev != cxl_topology_host);
+       up_read(&cxl_topology_rwsem);
+}
+EXPORT_SYMBOL_NS_GPL(put_cxl_topology_host, CXL);
+
 /**
  * __cxl_driver_register - register a driver for the cxl bus

* @cxl_drv: cxl driver structure to attach

drivers/cxl/cxl.h +4 -0
index 5e2e93451928..bd7b008207a4 100644
@@ -302,6 +302,10 @@ struct cxl_decoder *cxl_decoder_alloc(struct
 cxl_port *port, int nr_targets);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
+void set_cxl_topology_host(struct device *dev);
+struct device *get_cxl_topology_host(void);
+void put_cxl_topology_host(struct device *dev);
+
 extern struct bus_type cxl_bus_type;
 
Quoted 1 line

struct cxl_driver {

As you've seen by now I do implement something like that you have below in a later patch. I believe it will still be nice to have, but I haven't read through all of your feedback yet. So I might change my mind.

#30 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 25 lines · Ben Widawsky
Quoted 15 lines · Dan Williams
Quoted 14 lines · Ben Widawsky

obj-$(CONFIG_CXL_PCI) += cxl_pci.o
obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o

(quoted diff) +1 -0
@@ -7,3 +8,4 @@ obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 cxl_pci-y := pci.o
 cxl_acpi-y := acpi.o
 cxl_pmem-y := pmem.o
+cxl_port-y := port.o
drivers/cxl/acpi.c +0 -0
index b972abc9f6ef..d61397055e9f 100644
@@ -491,3 +491,4 @@ static struct platform_driver cxl_acpi_driver = {
Quoted 8 lines

drivers/cxl/acpi.c static struct platform_driver cxl_acpi_driver = { · line 494

Quoted 4 lines
drivers/cxl/acpi.c +1 -0
 module_platform_driver(cxl_acpi_driver);
 MODULE_LICENSE("GPL v2");
 MODULE_IMPORT_NS(CXL);
+MODULE_SOFTDEP("pre: cxl_port");

Why does cxl_acpi depend on cxl_port being loaded at this point? I think this wants to wait for a future patch where cxl_acpi uses port driver services.

Quoted 5 lines

As of this patch it does use port driver services. cxl/acpi: Map single port host bridge component registers made that the case.

[snip]

My mistake... I had the order changed locally. Ignore this comment.

#31 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

In reply to Ben Widawsky
Quoted 21 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644
Quoted 9 lines

drivers/cxl/Makefile line 3

Quoted 4 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Quoted 3 lines

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

[..]

Quoted 9 lines
Quoted 6 lines

drivers/cxl/Makefile line 4

Quoted 1 line
drivers/cxl/Makefile +1 -0
+static inline int cxl_hdm_decoder_ig(u32 ctrl)

No need for plain inline in C files.

It's not clear why this simple helper needs a "cxl_hdm_decoder" namespace prefix?

I had a patch to share this with acpi driver at one point, but I dropped it. Do you care if I merge those two decoders, or just rename?

Not sure what you mean by "merge"?

Quoted 11 lines
Quoted 9 lines

drivers/cxl/Makefile

Quoted 7 lines
drivers/cxl/Makefile +5 -0
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl);
+
+       return 8 + val;
+}

Why is this return a power of 2 value...

I don't understand this comment.

Isn't this returning an IG as a bit-shift value? The "iw" is returning bytes and I'm proposing they both be bytes.

Quoted 84 lines
Quoted 74 lines

drivers/cxl/Makefile

Quoted 8 lines
drivers/cxl/Makefile +6 -0
+
+static inline int cxl_hdm_decoder_iw(u32 ctrl)
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl);
+
+       return 1 << val;

...while this one is converted to absolute values.

These could just be:

unsigned int to_interleave_granularity(u32 ctrl)
unsigned int to_interleave_ways(u32 ctrl)

...and return units in bytes.

Quoted 19 lines
drivers/cxl/Makefile +19 -0
+}
+
+static void get_caps(struct cxl_port *port, struct cxl_port_data *cpd)
+{
+       void __iomem *hdm_decoder = cpd->regs.hdm_decoder;
+       struct port_caps *caps = &cpd->caps;
+       u32 hdm_cap;
+
+       hdm_cap = readl(hdm_decoder + CXL_HDM_DECODER_CAP_OFFSET);
+
+       caps->count = cxl_hdm_decoder_count(hdm_cap);
+       caps->tc = FIELD_GET(CXL_HDM_DECODER_TARGET_COUNT_MASK, hdm_cap);
+       caps->interleave11_8 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_11_8, hdm_cap);
+       caps->interleave14_12 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap);
+}
+
+static int map_regs(struct cxl_port *port, void __iomem *crb,

drivers/cxl/Makefile

Quoted 10 lines
drivers/cxl/Makefile +10 -0
+                   struct cxl_port_data *cpd)
+{
+       struct cxl_register_map map;
+       struct cxl_component_reg_map *comp_map = &map.component_map;
+
+       cxl_probe_component_regs(&port->dev, crb, comp_map);
+       if (!comp_map->hdm_decoder.valid) {
+               dev_err(&port->dev, "HDM decoder registers invalid\n");
+               return -ENXIO;
+       }

Perhaps promote cxl_probe_regs() from the cxl_pci to the core and make it take a dev instead of a pdev, then you can do:

cxl_probe_regs(&port_dev->dev, CXL_REGLOC_RBI_COMPONENT)

...instead of open coding it again?

Quoted 19 lines
drivers/cxl/Makefile +19 -0
+
+       cpd->regs.hdm_decoder = crb + comp_map->hdm_decoder.offset;
+
+       return 0;
+}
+
+static u64 get_decoder_size(void __iomem *hdm_decoder, int n)
+{
+       u32 ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(n));
+
+       if (!!FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+               return 0;
+
+       return ioread64_hi_lo(hdm_decoder +
+                             CXL_HDM_DECODER0_SIZE_LOW_OFFSET(n));
+}
+
+static bool is_endpoint_port(struct cxl_port *port)
+{
Quoted 9 lines

drivers/cxl/Makefile

Quoted 5 lines
drivers/cxl/Makefile +5 -0
+       if (!port->uport->driver)
+               return false;
+
+       return to_cxl_drv(port->uport->driver)->id ==
+              CXL_DEVICE_MEMORY_EXPANDER;

Why does endpoint port device type determination need to reach through and read the driver type?

Quoted 2 lines

I couldn't figure out a better way at this point in enumeration. I'm open to suggestions.

list_empty(&port->dports)?

Quoted 20 lines

[snip]

Quoted 7 lines

drivers/cxl/Makefile

Quoted 6 lines
drivers/cxl/Makefile +6 -0
+
+       /*
+        * Enable HDM decoders for this port.
+        *
+        * FIXME: If the component was using DVSEC range registers for decode,
+        * this will destroy that.
Quoted 9 lines

Yeah, definitely need to check that before this patch can move forward. Perhaps a port should not even be registered if DVSEC Memory_Size && Mem_Enable are non zero, that device is explicitly opting out of being a part of the CXL 2.0 subsystem hierarchy. However, we might still need to track it and potentially reserve it out of CFMWS capacity to make sure nothing else collides with it. I'll also note that "ECN: Devices operating in CXL 1.1 mode with no RCRB" was recently published that reads on what the driver should do here.

Quoted 5 lines

I believe we want to create the port since we might decide to reset and want control back over it and as you said, safety check other things. The reason it was left as a FIXME is because this belongs in the PCI driver which I didn't really want to touch at this time. I will go back and add that for the next version.

Ok.

Quoted 13 lines
Quoted 6 lines

drivers/cxl/Makefile

Quoted 5 lines
drivers/cxl/Makefile +4 -0
+        */
+       ctrl = readl(portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
+       ctrl |= CXL_HDM_DECODER_ENABLE;
+       writel(ctrl, portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
Quoted 6 lines

I feel like that if the driver finds it enabled it should leave it enabled at ->remove() time, as you have it here, as BIOS might not be expecting the OS to disable a decoder it set up. However, if the driver actually does the enable, then it should pair it with a disable at the end of time, so not a blind enable, but one that conditionally arranges for the unwind.

Quoted 3 lines

My thought was that once we enumerate a port, all of it's architectural state belongs to the OS. For us that means blanket enable/disable. I don't feel strongly about this.

I think the driver needs to tread carefully when it comes to potential BIOS interactions. The minimum BIOS collision would be to not toggle the enable off if the OS cannot assert that it set it. A more precise policy would be checking if the device contributes to any EFI memory map ranges, or any locked CFMWS entries.

[..]

Quoted 15 lines
Quoted 15 lines
drivers/cxl/cxl.h +4 -0
index 5e2e93451928..bd7b008207a4 100644
@@ -302,6 +302,10 @@ struct cxl_decoder *cxl_decoder_alloc(struct
 cxl_port *port, int nr_targets);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
+void set_cxl_topology_host(struct device *dev);
+struct device *get_cxl_topology_host(void);
+void put_cxl_topology_host(struct device *dev);
+
 extern struct bus_type cxl_bus_type;
 
Quoted 5 lines
Quoted 1 line

struct cxl_driver {

As you've seen by now I do implement something like that you have below in a later patch. I believe it will still be nice to have, but I haven't read through all of your feedback yet. So I might change my mind.

The main difference I proposed is a {get,put}_cxl_topology_host() to wrap topology operations in a rwsem.

#32 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 22 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 13 lines

drivers/cxl/Makefile line 3

Quoted 8 lines
drivers/cxl/Makefile +1 -0
index cf07ae6cea17..40b386aaedf7 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Quoted 8 lines
Quoted 3 lines

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

Can't argue with idiomisms ;-)

Quoted 6 lines

[..]

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+static inline int cxl_hdm_decoder_ig(u32 ctrl)

No need for plain inline in C files.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

It's not clear why this simple helper needs a "cxl_hdm_decoder" namespace prefix?

I had a patch to share this with acpi driver at one point, but I dropped it. Do you care if I merge those two decoders, or just rename?

Not sure what you mean by "merge"?

To have a unified function for both the cxl_acpi driver and the cxl_port driver.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

drivers/cxl/Makefile

Quoted 7 lines
drivers/cxl/Makefile +5 -0
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl);
+
+       return 8 + val;
+}

Why is this return a power of 2 value...

Quoted 5 lines
Quoted 1 line

I don't understand this comment.

Isn't this returning an IG as a bit-shift value? The "iw" is returning bytes and I'm proposing they both be bytes.

IG is definitely wrong since it's documented in struct cxl_decoder as data stride. I will fix that. I don't follow what you mean by "iw" is returning bytes. It's returning the number of ways.

Quoted 84 lines
Quoted 84 lines
Quoted 83 lines

drivers/cxl/Makefile

Quoted 8 lines
drivers/cxl/Makefile +6 -0
+
+static inline int cxl_hdm_decoder_iw(u32 ctrl)
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl);
+
+       return 1 << val;

...while this one is converted to absolute values.

These could just be:

unsigned int to_interleave_granularity(u32 ctrl)
unsigned int to_interleave_ways(u32 ctrl)

...and return units in bytes.

Quoted 19 lines
drivers/cxl/Makefile +19 -0
+}
+
+static void get_caps(struct cxl_port *port, struct cxl_port_data *cpd)
+{
+       void __iomem *hdm_decoder = cpd->regs.hdm_decoder;
+       struct port_caps *caps = &cpd->caps;
+       u32 hdm_cap;
+
+       hdm_cap = readl(hdm_decoder + CXL_HDM_DECODER_CAP_OFFSET);
+
+       caps->count = cxl_hdm_decoder_count(hdm_cap);
+       caps->tc = FIELD_GET(CXL_HDM_DECODER_TARGET_COUNT_MASK, hdm_cap);
+       caps->interleave11_8 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_11_8, hdm_cap);
+       caps->interleave14_12 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap);
+}
+
+static int map_regs(struct cxl_port *port, void __iomem *crb,

drivers/cxl/Makefile

Quoted 10 lines
drivers/cxl/Makefile +10 -0
+                   struct cxl_port_data *cpd)
+{
+       struct cxl_register_map map;
+       struct cxl_component_reg_map *comp_map = &map.component_map;
+
+       cxl_probe_component_regs(&port->dev, crb, comp_map);
+       if (!comp_map->hdm_decoder.valid) {
+               dev_err(&port->dev, "HDM decoder registers invalid\n");
+               return -ENXIO;
+       }

Perhaps promote cxl_probe_regs() from the cxl_pci to the core and make it take a dev instead of a pdev, then you can do:

cxl_probe_regs(&port_dev->dev, CXL_REGLOC_RBI_COMPONENT)

...instead of open coding it again?

Quoted 14 lines
drivers/cxl/Makefile +14 -0
+
+       cpd->regs.hdm_decoder = crb + comp_map->hdm_decoder.offset;
+
+       return 0;
+}
+
+static u64 get_decoder_size(void __iomem *hdm_decoder, int n)
+{
+       u32 ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(n));
+
+       if (!!FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+               return 0;
+
+       return ioread64_hi_lo(hdm_decoder +

drivers/cxl/Makefile

Quoted 10 lines
drivers/cxl/Makefile +10 -0
+                             CXL_HDM_DECODER0_SIZE_LOW_OFFSET(n));
+}
+
+static bool is_endpoint_port(struct cxl_port *port)
+{
+       if (!port->uport->driver)
+               return false;
+
+       return to_cxl_drv(port->uport->driver)->id ==
+              CXL_DEVICE_MEMORY_EXPANDER;

Why does endpoint port device type determination need to reach through and read the driver type?

Quoted 5 lines
Quoted 2 lines

I couldn't figure out a better way at this point in enumeration. I'm open to suggestions.

list_empty(&port->dports)?

That seems obvious enough. In the v2 series it's possible due to failures for a switch port to also have list_empty(&port->dports). Should ports not get added if they have 0 dports (unless they're endpoints)?

Quoted 46 lines
Quoted 21 lines

[snip]

Quoted 7 lines

drivers/cxl/Makefile

Quoted 6 lines
drivers/cxl/Makefile +6 -0
+
+       /*
+        * Enable HDM decoders for this port.
+        *
+        * FIXME: If the component was using DVSEC range registers for decode,
+        * this will destroy that.
Quoted 9 lines

Yeah, definitely need to check that before this patch can move forward. Perhaps a port should not even be registered if DVSEC Memory_Size && Mem_Enable are non zero, that device is explicitly opting out of being a part of the CXL 2.0 subsystem hierarchy. However, we might still need to track it and potentially reserve it out of CFMWS capacity to make sure nothing else collides with it. I'll also note that "ECN: Devices operating in CXL 1.1 mode with no RCRB" was recently published that reads on what the driver should do here.

Quoted 5 lines

I believe we want to create the port since we might decide to reset and want control back over it and as you said, safety check other things. The reason it was left as a FIXME is because this belongs in the PCI driver which I didn't really want to touch at this time. I will go back and add that for the next version.

Ok.

Quoted 13 lines
Quoted 6 lines

drivers/cxl/Makefile

Quoted 5 lines
drivers/cxl/Makefile +4 -0
+        */
+       ctrl = readl(portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
+       ctrl |= CXL_HDM_DECODER_ENABLE;
+       writel(ctrl, portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
Quoted 6 lines

I feel like that if the driver finds it enabled it should leave it enabled at ->remove() time, as you have it here, as BIOS might not be expecting the OS to disable a decoder it set up. However, if the driver actually does the enable, then it should pair it with a disable at the end of time, so not a blind enable, but one that conditionally arranges for the unwind.

Quoted 3 lines

My thought was that once we enumerate a port, all of it's architectural state belongs to the OS. For us that means blanket enable/disable. I don't feel strongly about this.

Quoted 5 lines

I think the driver needs to tread carefully when it comes to potential BIOS interactions. The minimum BIOS collision would be to not toggle the enable off if the OS cannot assert that it set it. A more precise policy would be checking if the device contributes to any EFI memory map ranges, or any locked CFMWS entries.

Locked == bit 4, fixed, right? The more precise policy makes sense to me, though I still wonder what happens if we reset a device for that case?

I'll work on the more precise version.

Quoted 17 lines

[..]

Quoted 15 lines
Quoted 15 lines
drivers/cxl/cxl.h +4 -0
index 5e2e93451928..bd7b008207a4 100644
@@ -302,6 +302,10 @@ struct cxl_decoder *cxl_decoder_alloc(struct
 cxl_port *port, int nr_targets);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
 
+void set_cxl_topology_host(struct device *dev);
+struct device *get_cxl_topology_host(void);
+void put_cxl_topology_host(struct device *dev);
+
 extern struct bus_type cxl_bus_type;
 
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

struct cxl_driver {

As you've seen by now I do implement something like that you have below in a later patch. I believe it will still be nice to have, but I haven't read through all of your feedback yet. So I might change my mind.

The main difference I proposed is a {get,put}_cxl_topology_host() to wrap topology operations in a rwsem.

Makes sense. I needed a topology lock eventually anyway.

#33 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 28 lines · Ben Widawsky
Quoted 26 lines · Dan Williams
Quoted 24 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644
Quoted 9 lines

drivers/cxl/Makefile line 3

Quoted 4 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

Quoted 6 lines
Quoted 4 lines

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

Can't argue with idiomisms ;-)

It's idiomatic because it's extensible. I expect type-2 drivers to select port services as well.

Quoted 10 lines
Quoted 10 lines

[..]

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

+static inline int cxl_hdm_decoder_ig(u32 ctrl)

No need for plain inline in C files.

It's not clear why this simple helper needs a "cxl_hdm_decoder" namespace prefix?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I had a patch to share this with acpi driver at one point, but I dropped it. Do you care if I merge those two decoders, or just rename?

Not sure what you mean by "merge"?

To have a unified function for both the cxl_acpi driver and the cxl_port driver.

Ah, yeah, CFMWS_INTERLEAVE_WAYS and CFMWS_INTERLEAVE_GRANULARITY are doing the same work.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 10 lines

drivers/cxl/Makefile

Quoted 8 lines
drivers/cxl/Makefile +5 -0
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IG_MASK, ctrl);
+
+       return 8 + val;
+}

Why is this return a power of 2 value...

I don't understand this comment.

Quoted 7 lines
Quoted 3 lines

Isn't this returning an IG as a bit-shift value? The "iw" is returning bytes and I'm proposing they both be bytes.

IG is definitely wrong since it's documented in struct cxl_decoder as data stride. I will fix that. I don't follow what you mean by "iw" is returning bytes. It's returning the number of ways.

Sorry, some noise from me there. I should have said integer instead of encoded value.

CFMWS_INTERLEAVE_GRANULARITY has the same problem (another indicator that a unit test for all user facing values is needed to catch simple bugs like this).

Quoted 88 lines
Quoted 88 lines
Quoted 87 lines
Quoted 75 lines

drivers/cxl/Makefile

Quoted 9 lines
drivers/cxl/Makefile +6 -0
+
+static inline int cxl_hdm_decoder_iw(u32 ctrl)
+{
+       int val = FIELD_GET(CXL_HDM_DECODER0_CTRL_IW_MASK, ctrl);
+
+       return 1 << val;

...while this one is converted to absolute values.

These could just be:

unsigned int to_interleave_granularity(u32 ctrl)
unsigned int to_interleave_ways(u32 ctrl)

...and return units in bytes.

Quoted 19 lines
drivers/cxl/Makefile +19 -0
+}
+
+static void get_caps(struct cxl_port *port, struct cxl_port_data *cpd)
+{
+       void __iomem *hdm_decoder = cpd->regs.hdm_decoder;
+       struct port_caps *caps = &cpd->caps;
+       u32 hdm_cap;
+
+       hdm_cap = readl(hdm_decoder + CXL_HDM_DECODER_CAP_OFFSET);
+
+       caps->count = cxl_hdm_decoder_count(hdm_cap);
+       caps->tc = FIELD_GET(CXL_HDM_DECODER_TARGET_COUNT_MASK, hdm_cap);
+       caps->interleave11_8 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_11_8, hdm_cap);
+       caps->interleave14_12 =
+               FIELD_GET(CXL_HDM_DECODER_INTERLEAVE_14_12, hdm_cap);
+}
+
+static int map_regs(struct cxl_port *port, void __iomem *crb,

drivers/cxl/Makefile

Quoted 10 lines
drivers/cxl/Makefile +10 -0
+                   struct cxl_port_data *cpd)
+{
+       struct cxl_register_map map;
+       struct cxl_component_reg_map *comp_map = &map.component_map;
+
+       cxl_probe_component_regs(&port->dev, crb, comp_map);
+       if (!comp_map->hdm_decoder.valid) {
+               dev_err(&port->dev, "HDM decoder registers invalid\n");
+               return -ENXIO;
+       }

Perhaps promote cxl_probe_regs() from the cxl_pci to the core and make it take a dev instead of a pdev, then you can do:

cxl_probe_regs(&port_dev->dev, CXL_REGLOC_RBI_COMPONENT)

...instead of open coding it again?

Quoted 19 lines
drivers/cxl/Makefile +19 -0
+
+       cpd->regs.hdm_decoder = crb + comp_map->hdm_decoder.offset;
+
+       return 0;
+}
+
+static u64 get_decoder_size(void __iomem *hdm_decoder, int n)
+{
+       u32 ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(n));
+
+       if (!!FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))
+               return 0;
+
+       return ioread64_hi_lo(hdm_decoder +
+                             CXL_HDM_DECODER0_SIZE_LOW_OFFSET(n));
+}
+
+static bool is_endpoint_port(struct cxl_port *port)
+{
Quoted 9 lines

drivers/cxl/Makefile

Quoted 5 lines
drivers/cxl/Makefile +5 -0
+       if (!port->uport->driver)
+               return false;
+
+       return to_cxl_drv(port->uport->driver)->id ==
+              CXL_DEVICE_MEMORY_EXPANDER;

Why does endpoint port device type determination need to reach through and read the driver type?

I couldn't figure out a better way at this point in enumeration. I'm open to suggestions.

Quoted 6 lines
Quoted 2 lines

list_empty(&port->dports)?

That seems obvious enough. In the v2 series it's possible due to failures for a switch port to also have list_empty(&port->dports). Should ports not get added if they have 0 dports (unless they're endpoints)?

Sounds reasonable, I can't imagine a usable non-endpoint port that has no dports.

Quoted 47 lines
Quoted 47 lines
Quoted 22 lines

[snip]

Quoted 7 lines

drivers/cxl/Makefile

Quoted 6 lines
drivers/cxl/Makefile +6 -0
+
+       /*
+        * Enable HDM decoders for this port.
+        *
+        * FIXME: If the component was using DVSEC range registers for decode,
+        * this will destroy that.
Quoted 9 lines

Yeah, definitely need to check that before this patch can move forward. Perhaps a port should not even be registered if DVSEC Memory_Size && Mem_Enable are non zero, that device is explicitly opting out of being a part of the CXL 2.0 subsystem hierarchy. However, we might still need to track it and potentially reserve it out of CFMWS capacity to make sure nothing else collides with it. I'll also note that "ECN: Devices operating in CXL 1.1 mode with no RCRB" was recently published that reads on what the driver should do here.

Quoted 5 lines

I believe we want to create the port since we might decide to reset and want control back over it and as you said, safety check other things. The reason it was left as a FIXME is because this belongs in the PCI driver which I didn't really want to touch at this time. I will go back and add that for the next version.

Ok.

Quoted 16 lines
Quoted 6 lines

drivers/cxl/Makefile

Quoted 5 lines
drivers/cxl/Makefile +4 -0
+        */
+       ctrl = readl(portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
+       ctrl |= CXL_HDM_DECODER_ENABLE;
+       writel(ctrl, portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
Quoted 6 lines

I feel like that if the driver finds it enabled it should leave it enabled at ->remove() time, as you have it here, as BIOS might not be expecting the OS to disable a decoder it set up. However, if the driver actually does the enable, then it should pair it with a disable at the end of time, so not a blind enable, but one that conditionally arranges for the unwind.

My thought was that once we enumerate a port, all of it's architectural state belongs to the OS. For us that means blanket enable/disable. I don't feel strongly about this.

Quoted 8 lines
Quoted 5 lines

I think the driver needs to tread carefully when it comes to potential BIOS interactions. The minimum BIOS collision would be to not toggle the enable off if the OS cannot assert that it set it. A more precise policy would be checking if the device contributes to any EFI memory map ranges, or any locked CFMWS entries.

Locked == bit 4, fixed, right? The more precise policy makes sense to me, though I still wonder what happens if we reset a device for that case?

I expect it will be painful and likely crash the system if that device is contributing to active System RAM. There is definitely some more exclusion needed to coordinate with secondary bus reset on the PCI side. I can imagine someone trying to passthrough a CXL device to a VM unaware that it will trigger a reset and crash the system.

#34 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

In reply to Dan Williams
Quoted 22 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 13 lines

drivers/cxl/Makefile line 3

Quoted 8 lines
drivers/cxl/Makefile +1 -0
index cf07ae6cea17..40b386aaedf7 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Quoted 9 lines
Quoted 3 lines

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

Should CXL_MEM also select CXL_PCI? I don't see a way CXL_MEM can work without CXL_PCI.

[snip]

#35 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 28 lines · Ben Widawsky
Quoted 26 lines · Dan Williams
Quoted 24 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644
Quoted 9 lines

drivers/cxl/Makefile line 3

Quoted 4 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

Quoted 8 lines
Quoted 5 lines

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

Should CXL_MEM also select CXL_PCI? I don't see a way CXL_MEM can work without CXL_PCI.

The "select" dependency should be an "I use services from module X" relationship. CXL_MEM uses port services and can't operate unless that module is present. CXL_MEM does not strictly need CXL_PCI especially since drivers/cxl/core/memdev.c abandoned all PCI refernces.

#36 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 35 lines · Dan Williams
Quoted 34 lines · Ben Widawsky
Quoted 31 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644
Quoted 9 lines

drivers/cxl/Makefile line 3

Quoted 4 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Quoted 3 lines

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

Quoted 7 lines
Quoted 2 lines

Should CXL_MEM also select CXL_PCI? I don't see a way CXL_MEM can work without CXL_PCI.

The "select" dependency should be an "I use services from module X" relationship. CXL_MEM uses port services and can't operate unless that module is present. CXL_MEM does not strictly need CXL_PCI especially since drivers/cxl/core/memdev.c abandoned all PCI refernces.

The problem I see is cxl_mem devices have a soft dependency on cxl_pci. For example what we discussed regard DVSEC range registers. My plan was if the endpoint driver can't determine range registers aren't in use, it won't bind. It can't make this determination without cxl_pci. DOE has a similar implication though not fatal.

#37 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 45 lines · Ben Widawsky
Quoted 35 lines · Dan Williams
Quoted 34 lines · Ben Widawsky
Quoted 31 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644
Quoted 9 lines

drivers/cxl/Makefile line 3

Quoted 4 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Quoted 3 lines

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

Quoted 7 lines
Quoted 2 lines

Should CXL_MEM also select CXL_PCI? I don't see a way CXL_MEM can work without CXL_PCI.

The "select" dependency should be an "I use services from module X" relationship. CXL_MEM uses port services and can't operate unless that module is present. CXL_MEM does not strictly need CXL_PCI especially since drivers/cxl/core/memdev.c abandoned all PCI refernces.

Quoted 5 lines

The problem I see is cxl_mem devices have a soft dependency on cxl_pci. For example what we discussed regard DVSEC range registers. My plan was if the endpoint driver can't determine range registers aren't in use, it won't bind. It can't make this determination without cxl_pci. DOE has a similar implication though not fatal.

I think this gets back to my original preference of just making the port driver a part of the core rather than its own independent module for no real reason. In fact it makes the validation job harder because it expands the testing surface for different combinations of module loading and device binding.

For the specific issue of CXL_MEM dependencies on CXL_PCI, isn't CXL_PCI passing in cacheed data for CXL_MEM? CXL_MEM has no idea that CXL_PCI exists, but it certainly knows that CXL_PORT exists,

#38 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 58 lines · Dan Williams
Quoted 45 lines · Ben Widawsky
Quoted 35 lines · Dan Williams
Quoted 34 lines · Ben Widawsky
Quoted 31 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/cxl/Makefile +0 -0
index cf07ae6cea17..40b386aaedf7 100644
Quoted 9 lines

drivers/cxl/Makefile line 3

Quoted 4 lines
drivers/cxl/Makefile +1 -0
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CXL_BUS) += core/
+obj-$(CONFIG_CXL_MEM) += cxl_port.o

It feel odd that CONFIG_CXL_MEM builds cxl_port, why not have a CONFIG_CXL_PORT that is simply selected by CONFIG_CXL_MEM, or a CONFIG_CXL_PORT that defaults to the value of CONFIG_CXL_BUS?

Quoted 3 lines

Can you help me understand when CONFIG_CXL_MEM is useful when #CONFIG_CXL_PORT=n? I was unable to figure out such a case and so I tied the two together.

With a 'select' dependency it's impossible to have the CONFIG_CXL_PORT=n and CONFIG_CXL_MEM=m combination. The extra config symbol is for idiomatic (one config-symbol per module .ko) reasons to reflect the module dependency in the Kconfig.

Quoted 7 lines
Quoted 2 lines

Should CXL_MEM also select CXL_PCI? I don't see a way CXL_MEM can work without CXL_PCI.

The "select" dependency should be an "I use services from module X" relationship. CXL_MEM uses port services and can't operate unless that module is present. CXL_MEM does not strictly need CXL_PCI especially since drivers/cxl/core/memdev.c abandoned all PCI refernces.

Quoted 5 lines

The problem I see is cxl_mem devices have a soft dependency on cxl_pci. For example what we discussed regard DVSEC range registers. My plan was if the endpoint driver can't determine range registers aren't in use, it won't bind. It can't make this determination without cxl_pci. DOE has a similar implication though not fatal.

I think this gets back to my original preference of just making the port driver a part of the core rather than its own independent module for no real reason. In fact it makes the validation job harder because it expands the testing surface for different combinations of module loading and device binding.

Quoted 3 lines

For the specific issue of CXL_MEM dependencies on CXL_PCI, isn't CXL_PCI passing in cacheed data for CXL_MEM? CXL_MEM has no idea that CXL_PCI exists, but it certainly knows that CXL_PORT exists,

Thinking about it further, the current bar for entry in cxl_mem is there must as least be component registers. If CXL_PCI doesn't do that, nothing will proceed, so I think this is fine.

Since you brought it up, further down the road, I'm not opposed toward moving the port driver into core, but for now I like it as the separate module. Some of the topology host games we're playing now might have been avoidable, although I think they're not a bad idea anyway.

#39 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

In reply to Dan Williams
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 40 lines
(quoted diff) +39 -0
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;
+
+       devm_remove_action(host, cxld_unregister, &cxld->dev);
+       put_cxl_topology_host(host);
+}
+
+/**
+ * cxl_port_decoder_autoremove - remove decoders discovered by the port driver
+ * @cxld: decoder to remove
+ *
+ * CXL.mem requires an intact port / decoder topology from root level
+ * platform decoders to endpoint decoders. Arrange for decoders
+ * enumerated by the port driver to be removed when either the root is
+ * removed (in which the entire hierarchy is removed), or when an
+ * individual port is disabled, in which case only that port's
+ * sub-section of the hierarchy is removed.
+ */
+int cxl_port_decoder_autoremove(struct cxl_decoder *cxld)
+{
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       struct device *host = get_cxl_topology_host();
+       int rc;
+
+       if (!host)
+               return -ENODEV;
+
+       if (!port->dev.driver) {
+               rc = -ENXIO;
+               goto out;
+       }
+
+       rc = cxl_decoder_autoremove(host, cxld);

Review

Quoted 10 lines
(quoted diff) +10 -0
+       if (rc)
+               goto out;
+
+       rc = devm_add_action_or_reset(&port->dev, trigger_decoder_autoremove,
+                                       cxld);
+out:
+       put_cxl_topology_host(host);
+       return rc;
+}
+EXPORT_SYMBOL_NS_GPL(cxl_port_decoder_autoremove, CXL);

The only port which has decoders that aren't discovered by the port driver would be the "root port" because it's platform specific. However, that port still is treated similarly to the other ports. Therefore I don't see why you need cxl_decoder_autoremove() anymore. Could you please explain? I think the safety check in trigger_decoder_autoremove() makes this work for all cases.

[snip]

#40 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 55 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 48 lines
(quoted diff) +47 -0
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;
+
+       devm_remove_action(host, cxld_unregister, &cxld->dev);
+       put_cxl_topology_host(host);
+}
+
+/**
+ * cxl_port_decoder_autoremove - remove decoders discovered by the port driver
+ * @cxld: decoder to remove
+ *
+ * CXL.mem requires an intact port / decoder topology from root level
+ * platform decoders to endpoint decoders. Arrange for decoders
+ * enumerated by the port driver to be removed when either the root is
+ * removed (in which the entire hierarchy is removed), or when an
+ * individual port is disabled, in which case only that port's
+ * sub-section of the hierarchy is removed.
+ */
+int cxl_port_decoder_autoremove(struct cxl_decoder *cxld)
+{
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       struct device *host = get_cxl_topology_host();
+       int rc;
+
+       if (!host)
+               return -ENODEV;
+
+       if (!port->dev.driver) {
+               rc = -ENXIO;
+               goto out;
+       }
+
+       rc = cxl_decoder_autoremove(host, cxld);
+       if (rc)
+               goto out;
+
+       rc = devm_add_action_or_reset(&port->dev, trigger_decoder_autoremove,
+                                       cxld);
+out:
+       put_cxl_topology_host(host);
+       return rc;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_NS_GPL(cxl_port_decoder_autoremove, CXL);

The only port which has decoders that aren't discovered by the port driver would be the "root port" because it's platform specific. However, that port still is treated similarly to the other ports. Therefore I don't see why you need cxl_decoder_autoremove() anymore. Could you please explain? I think the safety check in trigger_decoder_autoremove() makes this work for all cases.

I don't think it's worth the games to explain why CXL sees fit to register (here comes your favorite argument...) non-idiomatic devm actions on devices that are not associated with the running device + driver. So as long as the port driver auto-unloads its child devices then we're golden. Is your concern that you want to have the CFMWS decoders registers from cxl_port and not cxl_acpi?

#41 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 65 lines · Dan Williams
Quoted 55 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 48 lines
(quoted diff) +47 -0
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;
+
+       devm_remove_action(host, cxld_unregister, &cxld->dev);
+       put_cxl_topology_host(host);
+}
+
+/**
+ * cxl_port_decoder_autoremove - remove decoders discovered by the port driver
+ * @cxld: decoder to remove
+ *
+ * CXL.mem requires an intact port / decoder topology from root level
+ * platform decoders to endpoint decoders. Arrange for decoders
+ * enumerated by the port driver to be removed when either the root is
+ * removed (in which the entire hierarchy is removed), or when an
+ * individual port is disabled, in which case only that port's
+ * sub-section of the hierarchy is removed.
+ */
+int cxl_port_decoder_autoremove(struct cxl_decoder *cxld)
+{
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       struct device *host = get_cxl_topology_host();
+       int rc;
+
+       if (!host)
+               return -ENODEV;
+
+       if (!port->dev.driver) {
+               rc = -ENXIO;
+               goto out;
+       }
+
+       rc = cxl_decoder_autoremove(host, cxld);
+       if (rc)
+               goto out;
+
+       rc = devm_add_action_or_reset(&port->dev, trigger_decoder_autoremove,
+                                       cxld);
+out:
+       put_cxl_topology_host(host);
+       return rc;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_NS_GPL(cxl_port_decoder_autoremove, CXL);

The only port which has decoders that aren't discovered by the port driver would be the "root port" because it's platform specific. However, that port still is treated similarly to the other ports. Therefore I don't see why you need cxl_decoder_autoremove() anymore. Could you please explain? I think the safety check in trigger_decoder_autoremove() makes this work for all cases.

Quoted 6 lines

I don't think it's worth the games to explain why CXL sees fit to register (here comes your favorite argument...) non-idiomatic devm actions on devices that are not associated with the running device + driver. So as long as the port driver auto-unloads its child devices then we're golden. Is your concern that you want to have the CFMWS decoders registers from cxl_port and not cxl_acpi?

Hey! I'm all for idioms when it makes sense. To me, one of the coolest things about working on a new subsystem is you get to define some of your own idioms, but anyway...

First, no I think the platform driver (cxl_acpi) should enumerate the platform decoders. I confused devm_remove_action() with devm_release_action(). I wonder though, if you made trigger_decoder_autoremove() call devm_release_action, would that be sufficient and then remove cxl_decoder_autoremove()?

#42 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 74 lines · Ben Widawsky
Quoted 65 lines · Dan Williams
Quoted 55 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 48 lines
(quoted diff) +47 -0
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;
+
+       devm_remove_action(host, cxld_unregister, &cxld->dev);
+       put_cxl_topology_host(host);
+}
+
+/**
+ * cxl_port_decoder_autoremove - remove decoders discovered by the port driver
+ * @cxld: decoder to remove
+ *
+ * CXL.mem requires an intact port / decoder topology from root level
+ * platform decoders to endpoint decoders. Arrange for decoders
+ * enumerated by the port driver to be removed when either the root is
+ * removed (in which the entire hierarchy is removed), or when an
+ * individual port is disabled, in which case only that port's
+ * sub-section of the hierarchy is removed.
+ */
+int cxl_port_decoder_autoremove(struct cxl_decoder *cxld)
+{
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       struct device *host = get_cxl_topology_host();
+       int rc;
+
+       if (!host)
+               return -ENODEV;
+
+       if (!port->dev.driver) {
+               rc = -ENXIO;
+               goto out;
+       }
+
+       rc = cxl_decoder_autoremove(host, cxld);
+       if (rc)
+               goto out;
+
+       rc = devm_add_action_or_reset(&port->dev, trigger_decoder_autoremove,
+                                       cxld);
+out:
+       put_cxl_topology_host(host);
+       return rc;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_NS_GPL(cxl_port_decoder_autoremove, CXL);

The only port which has decoders that aren't discovered by the port driver would be the "root port" because it's platform specific. However, that port still is treated similarly to the other ports. Therefore I don't see why you need cxl_decoder_autoremove() anymore. Could you please explain? I think the safety check in trigger_decoder_autoremove() makes this work for all cases.

Quoted 6 lines

I don't think it's worth the games to explain why CXL sees fit to register (here comes your favorite argument...) non-idiomatic devm actions on devices that are not associated with the running device + driver. So as long as the port driver auto-unloads its child devices then we're golden. Is your concern that you want to have the CFMWS decoders registers from cxl_port and not cxl_acpi?

Quoted 8 lines

Hey! I'm all for idioms when it makes sense. To me, one of the coolest things about working on a new subsystem is you get to define some of your own idioms, but anyway...

First, no I think the platform driver (cxl_acpi) should enumerate the platform decoders. I confused devm_remove_action() with devm_release_action(). I wonder though, if you made trigger_decoder_autoremove() call devm_release_action, would that be sufficient and then remove cxl_decoder_autoremove()?

devm_release_action() is not needed if the devm is allowed to fire naturally which was the realization I came to here:

https://lore.kernel.org/r/CAPcyv4gpA=DH0SQvRdmF6dY01mZ1S-gGEWTSDbb+0ajYtyNv0A@mail.gmail.com

I.e. I regret opening that pandora's box of registering "remote" devm actions vs typical local devm actions.

#43 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

Quoted 84 lines · Dan Williams
Quoted 74 lines · Ben Widawsky
Quoted 65 lines · Dan Williams
Quoted 55 lines · Ben Widawsky
Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 48 lines
(quoted diff) +47 -0
+void trigger_decoder_autoremove(void *data)
+{
+       struct cxl_decoder *cxld = data;
+       struct device *host = get_cxl_topology_host();
+
+       /* The topology host driver beat us to the punch */
+       if (!host)
+               return;
+
+       devm_remove_action(host, cxld_unregister, &cxld->dev);
+       put_cxl_topology_host(host);
+}
+
+/**
+ * cxl_port_decoder_autoremove - remove decoders discovered by the port driver
+ * @cxld: decoder to remove
+ *
+ * CXL.mem requires an intact port / decoder topology from root level
+ * platform decoders to endpoint decoders. Arrange for decoders
+ * enumerated by the port driver to be removed when either the root is
+ * removed (in which the entire hierarchy is removed), or when an
+ * individual port is disabled, in which case only that port's
+ * sub-section of the hierarchy is removed.
+ */
+int cxl_port_decoder_autoremove(struct cxl_decoder *cxld)
+{
+       struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+       struct device *host = get_cxl_topology_host();
+       int rc;
+
+       if (!host)
+               return -ENODEV;
+
+       if (!port->dev.driver) {
+               rc = -ENXIO;
+               goto out;
+       }
+
+       rc = cxl_decoder_autoremove(host, cxld);
+       if (rc)
+               goto out;
+
+       rc = devm_add_action_or_reset(&port->dev, trigger_decoder_autoremove,
+                                       cxld);
+out:
+       put_cxl_topology_host(host);
+       return rc;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_NS_GPL(cxl_port_decoder_autoremove, CXL);

The only port which has decoders that aren't discovered by the port driver would be the "root port" because it's platform specific. However, that port still is treated similarly to the other ports. Therefore I don't see why you need cxl_decoder_autoremove() anymore. Could you please explain? I think the safety check in trigger_decoder_autoremove() makes this work for all cases.

Quoted 6 lines

I don't think it's worth the games to explain why CXL sees fit to register (here comes your favorite argument...) non-idiomatic devm actions on devices that are not associated with the running device + driver. So as long as the port driver auto-unloads its child devices then we're golden. Is your concern that you want to have the CFMWS decoders registers from cxl_port and not cxl_acpi?

Quoted 8 lines

Hey! I'm all for idioms when it makes sense. To me, one of the coolest things about working on a new subsystem is you get to define some of your own idioms, but anyway...

First, no I think the platform driver (cxl_acpi) should enumerate the platform decoders. I confused devm_remove_action() with devm_release_action(). I wonder though, if you made trigger_decoder_autoremove() call devm_release_action, would that be sufficient and then remove cxl_decoder_autoremove()?

Quoted 7 lines

devm_release_action() is not needed if the devm is allowed to fire naturally which was the realization I came to here:

https://lore.kernel.org/r/CAPcyv4gpA=DH0SQvRdmF6dY01mZ1S-gGEWTSDbb+0ajYtyNv0A@mail.gmail.com

I.e. I regret opening that pandora's box of registering "remote" devm actions vs typical local devm actions.

I wasn't suggesting release() fixes anything, but it did seem like we could get rid of cxl_decoder_autoremove() if we switched it to release(). Must of this gets too complex for me too quickly though, so if you think it won't work I'm happy to leave it at that.

#44 lore raw

Re: [RFC PATCH v2 08/28] cxl/port: Introduce a port driver

In reply to Ben Widawsky
Quoted 4 lines · On Fri, 22 Oct 2021 11:36:49 -0700 Ben Widawsky <[email protected]> wrote:

The CXL port driver will be responsible for managing the decoder resources contained within the port. It will also provide APIs that other drivers will consume for managing these resources.

Quoted 8 lines

Since the port driver is responsible for instantiating new decoders, and it does so during probe(), a new API is needed to add decoders for callers which already hold the device lock of the port.

This patch has no functional change because no driver is registering new ports and the root ports that are already registered should be skipped.

Hi Ben, A few fairly generic comments on stuff noticed whilst catching up with discussion around this series.

Jonathan

Quoted 29 lines
drivers/cxl/cxl.h +9 -5
index 91b8fd54bc93..ad22caf9135c 100644
@@ -17,6 +17,9 @@
  * (port-driver, region-driver, nvdimm object-drivers... etc).
  */
 
+/* CXL 2.0 8.2.4 CXL Component Register Layout and Definition */
+#define CXL_COMPONENT_REG_BLOCK_SIZE SZ_64K
+
 /* CXL 2.0 8.2.5 CXL.cache and CXL.mem Registers*/
 #define CXL_CM_OFFSET 0x1000
 #define CXL_CM_CAP_HDR_OFFSET 0x0
@@ -36,11 +39,22 @@
 #define CXL_HDM_DECODER_CAP_OFFSET 0x0
 #define   CXL_HDM_DECODER_COUNT_MASK GENMASK(3, 0)
 #define   CXL_HDM_DECODER_TARGET_COUNT_MASK GENMASK(7, 4)
-#define CXL_HDM_DECODER0_BASE_LOW_OFFSET 0x10
-#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET 0x14
-#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET 0x18
-#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET 0x1c
-#define CXL_HDM_DECODER0_CTRL_OFFSET 0x20
+#define   CXL_HDM_DECODER_INTERLEAVE_11_8 BIT(8)
+#define   CXL_HDM_DECODER_INTERLEAVE_14_12 BIT(9)
+#define CXL_HDM_DECODER_CTRL_OFFSET 0x4
+#define   CXL_HDM_DECODER_ENABLE BIT(1)
+#define CXL_HDM_DECODER0_BASE_LOW_OFFSET(i) (0x20 * (i) + 0x10)
+#define CXL_HDM_DECODER0_BASE_HIGH_OFFSET(i) (0x20 * (i) + 0x14)

drivers/cxl/cxl.h line 57

Quoted 10 lines
drivers/cxl/cxl.h +10 -0
+#define CXL_HDM_DECODER0_SIZE_LOW_OFFSET(i) (0x20 * (i) + 0x18)
+#define CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(i) (0x20 * (i) + 0x1c)
+#define CXL_HDM_DECODER0_CTRL_OFFSET(i) (0x20 * (i) + 0x20)
+#define   CXL_HDM_DECODER0_CTRL_IG_MASK GENMASK(3, 0)
+#define   CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
+#define   CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
+#define   CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
+#define   CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
+#define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
+#define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)

CXL_HDM_DECODERX_TL_HIGH etc perhaps as no longer just decoder 0?

Quoted 5 lines
drivers/cxl/port.c new file +0 -0
new file mode 100644
index 000000000000..ebbfb72ae995

...

drivers/cxl/port.c line 100

Quoted 6 lines
drivers/cxl/port.c new file +6 -0
+
+static u64 get_decoder_size(void __iomem *hdm_decoder, int n)
+{
+	u32 ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(n));
+
+	if (!!FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl))

Drop the !! as doesn't do anything useful.

Quoted 9 lines
drivers/cxl/port.c new file +9 -0
+		return 0;
+
+	return ioread64_hi_lo(hdm_decoder +
+			      CXL_HDM_DECODER0_SIZE_LOW_OFFSET(n));
+}
+
+static bool is_endpoint_port(struct cxl_port *port)
+{
+	if (!port->uport->driver)

drivers/cxl/port.c line 119

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+		return false;
+
+	return to_cxl_drv(port->uport->driver)->id ==
+	       CXL_DEVICE_MEMORY_EXPANDER;
+}
+
+static int enumerate_hdm_decoders(struct cxl_port *port,
+				  struct cxl_port_data *portdata)
+{
+	int i = 0;

Don't init.

drivers/cxl/port.c line 122

Quoted 3 lines
drivers/cxl/port.c new file +3 -0
+
+	for (i = 0; i < portdata->caps.count; i++) {
+		int iw = 1, ig = 0, rc, target_count = portdata->caps.tc;

Use some more lines for this - it'll be easier to read!

drivers/cxl/port.c line 131

Quoted 9 lines
drivers/cxl/port.c new file +9 -0
+		void __iomem *hdm_decoder = portdata->regs.hdm_decoder;
+		enum cxl_decoder_type type = CXL_DECODER_EXPANDER;
+		struct resource res = DEFINE_RES_MEM(0, 0);
+		struct cxl_decoder *cxld;
+		int *target_map = NULL;
+		u64 size;
+
+		if (is_endpoint_port(port))
+			target_count = 0;

I'd rather just see a bool for this instead of using the value not allowed for other cases. Slightly more code, but no need for the comment in the previous patch.

Quoted 20 lines
drivers/cxl/port.c new file +20 -0
+
+		cxld = cxl_decoder_alloc(port, target_count);
+		if (IS_ERR(cxld)) {
+			dev_warn(&port->dev,
+				 "Failed to allocate the decoder\n");
+			return PTR_ERR(cxld);
+		}
+
+		size = get_decoder_size(hdm_decoder, i);
+		if (size != 0) {
+			int temp[CXL_DECODER_MAX_INTERLEAVE];
+			u64 target_list, base;
+			u32 ctrl;
+			int j;
+
+			target_map = temp;
+			ctrl = readl(hdm_decoder + CXL_HDM_DECODER0_CTRL_OFFSET(i));
+			base = ioread64_hi_lo(hdm_decoder + CXL_HDM_DECODER0_BASE_LOW_OFFSET(i));
+			res = (struct resource)DEFINE_RES_MEM(base, size);
+

drivers/cxl/port.c line 161

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+			cxld->flags = CXL_DECODER_F_EN;
+			iw = cxl_hdm_decoder_iw(ctrl);
+			ig = cxl_hdm_decoder_ig(ctrl);
+
+			if (FIELD_GET(CXL_HDM_DECODER0_CTRL_TYPE, ctrl) == 0)
+				type = CXL_DECODER_ACCELERATOR;
+
+			target_list = ioread64_hi_lo(hdm_decoder + CXL_HDM_DECODER0_TL_LOW(i));
+			for (j = 0; j < iw; j++)
+				target_map[j] = (target_list >> (j * 8)) & 0xff;

temp just went out of scope, and target_map is still in it...

Quoted 4 lines
drivers/cxl/port.c new file +4 -0
+		}
+
+		cxld->target_type = type;
+		cxld->res = res;

drivers/cxl/port.c line 175

Quoted 10 lines
drivers/cxl/port.c new file +10 -0
+		cxld->interleave_ways = iw;
+		cxld->interleave_granularity = ig;
+
+		rc = cxl_decoder_add_locked(cxld, target_map);
+		if (rc)
+			put_device(&cxld->dev);
+		else
+			rc = cxl_decoder_autoremove(port->uport->parent, cxld);
+		if (rc)
+			dev_err(&port->dev, "Failed to add decoder\n");

why not return an error?

Quoted 67 lines
drivers/cxl/port.c new file +67 -0
+	}
+
+	return 0;
+}
+
+static int cxl_port_probe(struct device *dev)
+{
+	struct cxl_port *port = to_cxl_port(dev);
+	struct cxl_port_data *portdata;
+	void __iomem *crb;
+	u32 ctrl;
+	int rc;
+
+	if (port->component_reg_phys == CXL_RESOURCE_NONE)
+		return 0;
+
+	portdata = devm_kzalloc(dev, sizeof(*portdata), GFP_KERNEL);
+	if (!portdata)
+		return -ENOMEM;
+
+	crb = devm_cxl_iomap_block(&port->dev, port->component_reg_phys,
+				   CXL_COMPONENT_REG_BLOCK_SIZE);
+	if (IS_ERR_OR_NULL(crb)) {
+		dev_err(&port->dev, "No component registers mapped\n");
+		return -ENXIO;
+	}
+
+	rc = map_regs(port, crb, portdata);
+	if (rc)
+		return rc;
+
+	get_caps(port, portdata);
+	if (portdata->caps.count == 0) {
+		dev_err(&port->dev, "Spec violation. Caps invalid\n");
+		return -ENXIO;
+	}
+
+	/*
+	 * Enable HDM decoders for this port.
+	 *
+	 * FIXME: If the component was using DVSEC range registers for decode,
+	 * this will destroy that.
+	 */
+	ctrl = readl(portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
+	ctrl |= CXL_HDM_DECODER_ENABLE;
+	writel(ctrl, portdata->regs.hdm_decoder + CXL_HDM_DECODER_CTRL_OFFSET);
+
+	rc = enumerate_hdm_decoders(port, portdata);
+	if (rc) {
+		dev_err(&port->dev, "Couldn't enumerate decoders (%d)\n", rc);
+		return rc;
+	}
+
+	dev_set_drvdata(dev, portdata);
+	return 0;
+}
+
+static struct cxl_driver cxl_port_driver = {
+	.name = "cxl_port",
+	.probe = cxl_port_probe,
+	.id = CXL_DEVICE_PORT,
+};
+module_cxl_driver(cxl_port_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_IMPORT_NS(CXL);
+MODULE_ALIAS_CXL(CXL_DEVICE_PORT);
#45 lore raw

RFC PATCH v2 9/28 cxl/acpi: Map single port host bridge component registers

In reply to Ben Widawsky

Now that the port driver exists and is able to do proper decoder enumeration of the component registers, it becomes trivial to use that for host bridge uports. For reasons out of scope, a functional change would be visible if the HDM decoder was programmed by BIOS to values other than the full address range. Similarly if a type2 device was connected to this root port and programmed by BIOS, that can now be acted upon accordingly.

---
 drivers/cxl/acpi.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +24 -1
index d61397055e9f..8cca0814dfb8 100644
@@ -280,12 +280,14 @@ static int add_host_bridge_uport(struct device *match, void *arg)
 	struct cxl_port *root_port = arg;
 	struct device *host = root_port->dev.parent;
 	struct acpi_device *bridge = to_cxl_host_bridge(host, match);
+	struct cxl_component_reg_map map;
 	struct acpi_pci_root *pci_root;
 	struct cxl_walk_context ctx;
 	int single_port_map[1], rc;
 	struct cxl_decoder *cxld;
 	struct cxl_dport *dport;
 	struct cxl_port *port;
+	void __iomem *crb;
 
 	if (!bridge)
 		return 0;
@@ -318,10 +320,31 @@ static int add_host_bridge_uport(struct device *match, void *arg)
 		return -ENODEV;
 	if (ctx.error)
 		return ctx.error;
+	/*
+	 * If the host bridge has more than 1 root port, it must have registers
+	 * controlling the HDM decoders. Those will be enumerated by the port
+	 * driver.
+	 */
 	if (ctx.count > 1)
 		return 0;
 
-	/* TODO: Scan CHBCR for HDM Decoder resources */
+	/*
+	 * If the single ported host bridge has a component register block,
+	 * simply let the port driver handle the decoder enumeration.
+	 *
+	 * Host bridge component registers live in the system's physical address
+	 * space.
+	 */
+	crb = ioremap(dport->component_reg_phys, CXL_COMPONENT_REG_BLOCK_SIZE);
+	if (crb) {
+		cxl_probe_component_regs(&root_port->dev, crb, &map);
+		iounmap(crb);
+		if (map.hdm_decoder.valid) {
+			dev_dbg(host,
+				"Found single port host bridge with component registers\n");
+			return 0;
+		}
+	}
 
 	/*
 	 * Per the CXL specification (8.2.5.12 CXL HDM Decoder Capability
Signature · 1 line
2.33.1
#46 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 3 lines · Ben Widawsky

Now that the port driver exists and is able to do proper decoder enumeration of the component registers, it becomes trivial to use that

This is the second occurrence of "becomes trivial" in this series, it's distracting because it immediately sets off alarm bells that the changelog is not in:

Background
Problem
Solution

...format.

Quoted 5 lines

for host bridge uports. For reasons out of scope, a functional change would be visible if the HDM decoder was programmed by BIOS to values other than the full address range. Similarly if a type2 device was connected to this root port and programmed by BIOS, that can now be acted upon accordingly.

I would reserve discussion of "no functional change" for patches that are pure cleanup. In this case this patch will cause the kernel to behave differently when other conditions are met.

Quoted 36 lines
---
 drivers/cxl/acpi.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +7 -0
index d61397055e9f..8cca0814dfb8 100644
@@ -280,12 +280,14 @@ static int add_host_bridge_uport(struct device *match, void *arg)
        struct cxl_port *root_port = arg;
        struct device *host = root_port->dev.parent;
        struct acpi_device *bridge = to_cxl_host_bridge(host, match);
+       struct cxl_component_reg_map map;
        struct acpi_pci_root *pci_root;
        struct cxl_walk_context ctx;
        int single_port_map[1], rc;
        struct cxl_decoder *cxld;
        struct cxl_dport *dport;
        struct cxl_port *port;
+       void __iomem *crb;
 
        if (!bridge)
                return 0;
@@ -318,10 +320,31 @@ static int add_host_bridge_uport(struct device *match, void *arg)
                return -ENODEV;
        if (ctx.error)
                return ctx.error;
+       /*
+        * If the host bridge has more than 1 root port, it must have registers
+        * controlling the HDM decoders. Those will be enumerated by the port
+        * driver.
+        */
        if (ctx.count > 1)
                return 0;

drivers/cxl/acpi.c static int add_host_bridge_uport(struct device *match, void *arg) · line 338

Quoted 10 lines
drivers/cxl/acpi.c +8 -1
 
-       /* TODO: Scan CHBCR for HDM Decoder resources */
+       /*
+        * If the single ported host bridge has a component register block,
+        * simply let the port driver handle the decoder enumeration.
+        *
+        * Host bridge component registers live in the system's physical address
+        * space.
+        */
+       crb = ioremap(dport->component_reg_phys, CXL_COMPONENT_REG_BLOCK_SIZE);

This looks broken, the driver for the port should be mapping dport component registers to offer services to upper layers.

There's also a missing iounmap.

Quoted 15 lines
drivers/cxl/acpi.c +9 -1
+       if (crb) {
+               cxl_probe_component_regs(&root_port->dev, crb, &map);
+               iounmap(crb);
+               if (map.hdm_decoder.valid) {
+                       dev_dbg(host,
+                               "Found single port host bridge with component registers\n");
+                       return 0;
+               }
+       }
 
        /*
         * Per the CXL specification (8.2.5.12 CXL HDM Decoder Capability
--

2.33.1

#47 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 9 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Now that the port driver exists and is able to do proper decoder enumeration of the component registers, it becomes trivial to use that

This is the second occurrence of "becomes trivial" in this series, it's distracting because it immediately sets off alarm bells that the changelog is not in:

Quoted 5 lines

Background
Problem
Solution

...format.

Let me make sure I understand.

Background: Now that the port driver exists...
Problem: host bridge uports can use the port driver [but don't yet]
Solution: This patch (the description is indeed missing).

Is your point that I didn't document a solution, or something else?

Quoted 10 lines
Quoted 6 lines

for host bridge uports. For reasons out of scope, a functional change would be visible if the HDM decoder was programmed by BIOS to values other than the full address range. Similarly if a type2 device was connected to this root port and programmed by BIOS, that can now be acted upon accordingly.

I would reserve discussion of "no functional change" for patches that are pure cleanup. In this case this patch will cause the kernel to behave differently when other conditions are met.

True. I will remove that.

Quoted 51 lines
Quoted 37 lines
---
 drivers/cxl/acpi.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +7 -0
index d61397055e9f..8cca0814dfb8 100644
@@ -280,12 +280,14 @@ static int add_host_bridge_uport(struct device *match, void *arg)
        struct cxl_port *root_port = arg;
        struct device *host = root_port->dev.parent;
        struct acpi_device *bridge = to_cxl_host_bridge(host, match);
+       struct cxl_component_reg_map map;
        struct acpi_pci_root *pci_root;
        struct cxl_walk_context ctx;
        int single_port_map[1], rc;
        struct cxl_decoder *cxld;
        struct cxl_dport *dport;
        struct cxl_port *port;
+       void __iomem *crb;
 
        if (!bridge)
                return 0;
@@ -318,10 +320,31 @@ static int add_host_bridge_uport(struct device *match, void *arg)
                return -ENODEV;
        if (ctx.error)
                return ctx.error;
+       /*
+        * If the host bridge has more than 1 root port, it must have registers
+        * controlling the HDM decoders. Those will be enumerated by the port
+        * driver.
+        */
        if (ctx.count > 1)
                return 0;

drivers/cxl/acpi.c static int add_host_bridge_uport(struct device *match, void *arg) · line 338

Quoted 10 lines
drivers/cxl/acpi.c +8 -1
 
-       /* TODO: Scan CHBCR for HDM Decoder resources */
+       /*
+        * If the single ported host bridge has a component register block,
+        * simply let the port driver handle the decoder enumeration.
+        *
+        * Host bridge component registers live in the system's physical address
+        * space.
+        */
+       crb = ioremap(dport->component_reg_phys, CXL_COMPONENT_REG_BLOCK_SIZE);

This looks broken, the driver for the port should be mapping dport component registers to offer services to upper layers.

Quoted 2 lines

There's also a missing iounmap.

Where's the missing iounmap? The port driver does what you say, but I need a way to shortcircuit the case where the root port doesn't have component registers which you've previously documented as allowed by spec. How would you recommend doing that?

Quoted 15 lines
Quoted 15 lines
drivers/cxl/acpi.c +9 -1
+       if (crb) {
+               cxl_probe_component_regs(&root_port->dev, crb, &map);
+               iounmap(crb);
+               if (map.hdm_decoder.valid) {
+                       dev_dbg(host,
+                               "Found single port host bridge with component registers\n");
+                       return 0;
+               }
+       }
 
        /*
         * Per the CXL specification (8.2.5.12 CXL HDM Decoder Capability
--

2.33.1

#48 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 17 lines · Ben Widawsky
Quoted 9 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Now that the port driver exists and is able to do proper decoder enumeration of the component registers, it becomes trivial to use that

This is the second occurrence of "becomes trivial" in this series, it's distracting because it immediately sets off alarm bells that the changelog is not in:

Quoted 5 lines

Background
Problem
Solution

...format.

Quoted 7 lines

Let me make sure I understand.

Background: Now that the port driver exists...
Problem: host bridge uports can use the port driver [but don't yet]
Solution: This patch (the description is indeed missing).

Is your point that I didn't document a solution, or something else?

Why do single port host bridge registers need to be mapped?

Why does the cxl_acpi driver need to do it and not the just introduced port driver?

This patch seems to be saying, map these because they can be mapped.

Quoted 66 lines
Quoted 9 lines
Quoted 8 lines

for host bridge uports. For reasons out of scope, a functional change would be visible if the HDM decoder was programmed by BIOS to values other than the full address range. Similarly if a type2 device was connected to this root port and programmed by BIOS, that can now be acted upon accordingly.

Quoted 3 lines

I would reserve discussion of "no functional change" for patches that are pure cleanup. In this case this patch will cause the kernel to behave differently when other conditions are met.

True. I will remove that.

Quoted 51 lines
Quoted 37 lines
---
 drivers/cxl/acpi.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +7 -0
index d61397055e9f..8cca0814dfb8 100644
@@ -280,12 +280,14 @@ static int add_host_bridge_uport(struct device *match, void *arg)
        struct cxl_port *root_port = arg;
        struct device *host = root_port->dev.parent;
        struct acpi_device *bridge = to_cxl_host_bridge(host, match);
+       struct cxl_component_reg_map map;
        struct acpi_pci_root *pci_root;
        struct cxl_walk_context ctx;
        int single_port_map[1], rc;
        struct cxl_decoder *cxld;
        struct cxl_dport *dport;
        struct cxl_port *port;
+       void __iomem *crb;
 
        if (!bridge)
                return 0;
@@ -318,10 +320,31 @@ static int add_host_bridge_uport(struct device *match, void *arg)
                return -ENODEV;
        if (ctx.error)
                return ctx.error;
+       /*
+        * If the host bridge has more than 1 root port, it must have registers
+        * controlling the HDM decoders. Those will be enumerated by the port
+        * driver.
+        */
        if (ctx.count > 1)
                return 0;

drivers/cxl/acpi.c static int add_host_bridge_uport(struct device *match, void *arg) · line 338

Quoted 10 lines
drivers/cxl/acpi.c +8 -1
 
-       /* TODO: Scan CHBCR for HDM Decoder resources */
+       /*
+        * If the single ported host bridge has a component register block,
+        * simply let the port driver handle the decoder enumeration.
+        *
+        * Host bridge component registers live in the system's physical address
+        * space.
+        */
+       crb = ioremap(dport->component_reg_phys, CXL_COMPONENT_REG_BLOCK_SIZE);

This looks broken, the driver for the port should be mapping dport component registers to offer services to upper layers.

Quoted 4 lines
Quoted 2 lines

There's also a missing iounmap.

Where's the missing iounmap?

Oops, sorry, don't know why I missed that. Forgive the noise.

Quoted 4 lines

The port driver does what you say, but I need a way
to shortcircuit the case where the root port doesn't have component registers which you've previously documented as allowed by spec. How would you recommend doing that?

Perhaps cxl_port_probe() needs to special case the single dport case and just say, "no registers needed, single port == HDM passthrough".

Until there's a need to look at non-HDM registers I'd hold off on enabling this case. Probably that comes soon when considering IDE support, but no need to pre-enable that yet.

#49 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 111 lines · Dan Williams
Quoted 17 lines · Ben Widawsky
Quoted 9 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Now that the port driver exists and is able to do proper decoder enumeration of the component registers, it becomes trivial to use that

This is the second occurrence of "becomes trivial" in this series, it's distracting because it immediately sets off alarm bells that the changelog is not in:

Quoted 5 lines

Background
Problem
Solution

...format.

Quoted 7 lines

Let me make sure I understand.

Background: Now that the port driver exists...
Problem: host bridge uports can use the port driver [but don't yet]
Solution: This patch (the description is indeed missing).

Is your point that I didn't document a solution, or something else?

Why do single port host bridge registers need to be mapped?

Why does the cxl_acpi driver need to do it and not the just introduced port driver?

This patch seems to be saying, map these because they can be mapped.

Quoted 66 lines
Quoted 9 lines
Quoted 8 lines

for host bridge uports. For reasons out of scope, a functional change would be visible if the HDM decoder was programmed by BIOS to values other than the full address range. Similarly if a type2 device was connected to this root port and programmed by BIOS, that can now be acted upon accordingly.

Quoted 3 lines

I would reserve discussion of "no functional change" for patches that are pure cleanup. In this case this patch will cause the kernel to behave differently when other conditions are met.

True. I will remove that.

Quoted 51 lines
Quoted 37 lines
---
 drivers/cxl/acpi.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +7 -0
index d61397055e9f..8cca0814dfb8 100644
@@ -280,12 +280,14 @@ static int add_host_bridge_uport(struct device *match, void *arg)
        struct cxl_port *root_port = arg;
        struct device *host = root_port->dev.parent;
        struct acpi_device *bridge = to_cxl_host_bridge(host, match);
+       struct cxl_component_reg_map map;
        struct acpi_pci_root *pci_root;
        struct cxl_walk_context ctx;
        int single_port_map[1], rc;
        struct cxl_decoder *cxld;
        struct cxl_dport *dport;
        struct cxl_port *port;
+       void __iomem *crb;
 
        if (!bridge)
                return 0;
@@ -318,10 +320,31 @@ static int add_host_bridge_uport(struct device *match, void *arg)
                return -ENODEV;
        if (ctx.error)
                return ctx.error;
+       /*
+        * If the host bridge has more than 1 root port, it must have registers
+        * controlling the HDM decoders. Those will be enumerated by the port
+        * driver.
+        */
        if (ctx.count > 1)
                return 0;

drivers/cxl/acpi.c static int add_host_bridge_uport(struct device *match, void *arg) · line 338

Quoted 10 lines
drivers/cxl/acpi.c +8 -1
 
-       /* TODO: Scan CHBCR for HDM Decoder resources */
+       /*
+        * If the single ported host bridge has a component register block,
+        * simply let the port driver handle the decoder enumeration.
+        *
+        * Host bridge component registers live in the system's physical address
+        * space.
+        */
+       crb = ioremap(dport->component_reg_phys, CXL_COMPONENT_REG_BLOCK_SIZE);

This looks broken, the driver for the port should be mapping dport component registers to offer services to upper layers.

Quoted 4 lines
Quoted 2 lines

There's also a missing iounmap.

Where's the missing iounmap?

Oops, sorry, don't know why I missed that. Forgive the noise.

Quoted 4 lines

The port driver does what you say, but I need a way
to shortcircuit the case where the root port doesn't have component registers which you've previously documented as allowed by spec. How would you recommend doing that?

Quoted 6 lines

Perhaps cxl_port_probe() needs to special case the single dport case and just say, "no registers needed, single port == HDM passthrough".

Until there's a need to look at non-HDM registers I'd hold off on enabling this case. Probably that comes soon when considering IDE support, but no need to pre-enable that yet.

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

#50 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

#51 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 11 lines · Dan Williams

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

Without this code, a decoder is added for the full address range regardless if there is an existing programmed HDM decoder. With this code when the port driver probes this host bridge HDM component registers, everything will be enumerated by the normal flow.

This seemed easier than trying to have the port driver determine what cxl_acpi driver did and unwind that if there is a programmed decoder.

But now you're making me think I've missed something big...

#52 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 14 lines · Ben Widawsky
Quoted 11 lines · Dan Williams

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

Quoted 7 lines

Without this code, a decoder is added for the full address range regardless if there is an existing programmed HDM decoder. With this code when the port driver probes this host bridge HDM component registers, everything will be enumerated by the normal flow.

This seemed easier than trying to have the port driver determine what cxl_acpi driver did and unwind that if there is a programmed decoder.

I think this is more an argument to unify all decoder creation in the port driver. When the port driver sees a single dport with no HDM decoder registers it can create the passthrough decoder, if it sees single dport with HDM decoder registers then it can create a programmable decoder.

#53 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 20 lines · Dan Williams
Quoted 19 lines · Ben Widawsky
Quoted 11 lines · Dan Williams

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

Without this code, a decoder is added for the full address range regardless if there is an existing programmed HDM decoder. With this code when the port driver probes this host bridge HDM component registers, everything will be enumerated by the normal flow.

Quoted 8 lines
Quoted 2 lines

This seemed easier than trying to have the port driver determine what cxl_acpi driver did and unwind that if there is a programmed decoder.

I think this is more an argument to unify all decoder creation in the port driver. When the port driver sees a single dport with no HDM decoder registers it can create the passthrough decoder, if it sees single dport with HDM decoder registers then it can create a programmable decoder.

That too, though I'm not sure how useful that will be for us. I think the more likely case is BIOS is trying to hide some device (or switch) address space.

#54 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 25 lines · Ben Widawsky
Quoted 23 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 11 lines · Dan Williams

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

Without this code, a decoder is added for the full address range regardless if there is an existing programmed HDM decoder. With this code when the port driver probes this host bridge HDM component registers, everything will be enumerated by the normal flow.

This seemed easier than trying to have the port driver determine what cxl_acpi driver did and unwind that if there is a programmed decoder.

Quoted 7 lines
Quoted 5 lines

I think this is more an argument to unify all decoder creation in the port driver. When the port driver sees a single dport with no HDM decoder registers it can create the passthrough decoder, if it sees single dport with HDM decoder registers then it can create a programmable decoder.

That too, though I'm not sure how useful that will be for us.

?

This is about the proper place to enumerate decoders. The proposal in this patch is to do some part of the enumeration in cxl_acpi and some part in cxl_port, I'm saying push it all to cxl_port. I.e. it was a layering violation in retrospect to create a default passthrough decoder from cxl_acpi.

Quoted 2 lines

I think the more
likely case is BIOS is trying to hide some device (or switch) address space.

Not sure why that is relevant for the decision about where to place component register probing and passthrough decoder creation?

#55 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 34 lines · Dan Williams
Quoted 25 lines · Ben Widawsky
Quoted 23 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 11 lines · Dan Williams

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

Without this code, a decoder is added for the full address range regardless if there is an existing programmed HDM decoder. With this code when the port driver probes this host bridge HDM component registers, everything will be enumerated by the normal flow.

This seemed easier than trying to have the port driver determine what cxl_acpi driver did and unwind that if there is a programmed decoder.

Quoted 7 lines
Quoted 5 lines

I think this is more an argument to unify all decoder creation in the port driver. When the port driver sees a single dport with no HDM decoder registers it can create the passthrough decoder, if it sees single dport with HDM decoder registers then it can create a programmable decoder.

That too, though I'm not sure how useful that will be for us.

Quoted 7 lines

?

This is about the proper place to enumerate decoders. The proposal in this patch is to do some part of the enumeration in cxl_acpi and some part in cxl_port, I'm saying push it all to cxl_port. I.e. it was a layering violation in retrospect to create a default passthrough decoder from cxl_acpi.

I think it makes sense to have cxl_decoder_add() not called from anything but the port driver. So long as there's a way to determine it's a CXL 2.0 Root Port, which, it seems we'll have, it should be straight forward.

Quoted 6 lines
Quoted 3 lines

I think the more
likely case is BIOS is trying to hide some device (or switch) address space.

Not sure why that is relevant for the decision about where to place component register probing and passthrough decoder creation?

The comment wasn't about where, it was just a response as to how there's more motivation than just having a programmable decoder, you might find a locked decoder that has unexpected constraints.

#56 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 44 lines · Ben Widawsky
Quoted 34 lines · Dan Williams
Quoted 25 lines · Ben Widawsky
Quoted 23 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 11 lines · Dan Williams

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

Without this code, a decoder is added for the full address range regardless if there is an existing programmed HDM decoder. With this code when the port driver probes this host bridge HDM component registers, everything will be enumerated by the normal flow.

This seemed easier than trying to have the port driver determine what cxl_acpi driver did and unwind that if there is a programmed decoder.

Quoted 7 lines
Quoted 5 lines

I think this is more an argument to unify all decoder creation in the port driver. When the port driver sees a single dport with no HDM decoder registers it can create the passthrough decoder, if it sees single dport with HDM decoder registers then it can create a programmable decoder.

That too, though I'm not sure how useful that will be for us.

Quoted 7 lines

?

This is about the proper place to enumerate decoders. The proposal in this patch is to do some part of the enumeration in cxl_acpi and some part in cxl_port, I'm saying push it all to cxl_port. I.e. it was a layering violation in retrospect to create a default passthrough decoder from cxl_acpi.

Quoted 3 lines

I think it makes sense to have cxl_decoder_add() not called from anything but the port driver. So long as there's a way to determine it's a CXL 2.0 Root Port, which, it seems we'll have, it should be straight forward.

Does it need to be root port aware? I expect any single ported switch is treated the same, not that I expect single-ported switches to be a real thing, but I don't expect our port driver would care.

#57 lore raw

Re: [RFC PATCH v2 09/28] cxl/acpi: Map single port host bridge component registers

Quoted 45 lines · Dan Williams
Quoted 44 lines · Ben Widawsky
Quoted 41 lines · Dan Williams
Quoted 25 lines · Ben Widawsky
Quoted 23 lines · Dan Williams
Quoted 21 lines · Ben Widawsky
Quoted 11 lines · Dan Williams

On Tue, Nov 2, 2021 at 9:31 AM Ben Widawsky <[email protected]> wrote:
[..]

Quoted 2 lines

It seemed like a very simple thing to support given the port driver's existence so it was added to remove a TODO. However, I will drop it as you request.

I'm honestly asking the question why this is needed and more specifically why this is needed in this location?

In other words, how is this case different than typical component register probing that is done in the port driver itself? I would expect the TODO just gets deleted by the port driver addition.

Without this code, a decoder is added for the full address range regardless if there is an existing programmed HDM decoder. With this code when the port driver probes this host bridge HDM component registers, everything will be enumerated by the normal flow.

This seemed easier than trying to have the port driver determine what cxl_acpi driver did and unwind that if there is a programmed decoder.

Quoted 7 lines
Quoted 5 lines

I think this is more an argument to unify all decoder creation in the port driver. When the port driver sees a single dport with no HDM decoder registers it can create the passthrough decoder, if it sees single dport with HDM decoder registers then it can create a programmable decoder.

That too, though I'm not sure how useful that will be for us.

?

This is about the proper place to enumerate decoders. The proposal in this patch is to do some part of the enumeration in cxl_acpi and some part in cxl_port, I'm saying push it all to cxl_port. I.e. it was a layering violation in retrospect to create a default passthrough decoder from cxl_acpi.

Quoted 7 lines
Quoted 3 lines

I think it makes sense to have cxl_decoder_add() not called from anything but the port driver. So long as there's a way to determine it's a CXL 2.0 Root Port, which, it seems we'll have, it should be straight forward.

Does it need to be root port aware? I expect any single ported switch is treated the same, not that I expect single-ported switches to be a real thing, but I don't expect our port driver would care.

You're correct... I thought switch ports required HDM decoder registers even if single port, but I just checked and they are the same as the host bridge. So indeed the enumeration should only be special cased for the endpoint.

#58 lore raw

RFC PATCH v2 10/28 cxl/core: Store global list of root ports

In reply to Ben Widawsky

CXL root ports (the downstream port to a host bridge) are to be enumerated by a platform specific driver. In the case of ACPI compliant systems, this is like the cxl_acpi driver. Root ports are the first CXL spec defined component that can be "found" by that platform specific driver.

By storing a list of these root ports components in lower levels of the topology (switches and endpoints), have a mechanism to walk up their device hierarchy to find an enumerated root port. This will be necessary for region programming.

---
 drivers/cxl/acpi.c            |  4 ++--
 drivers/cxl/core/bus.c        | 34 +++++++++++++++++++++++++++++++++-
 drivers/cxl/cxl.h             |  5 ++++-
 tools/testing/cxl/mock_acpi.c |  4 ++--
 4 files changed, 41 insertions(+), 6 deletions(-)
drivers/cxl/acpi.c +2 -2
index 8cca0814dfb8..625c5d95b83f 100644
@@ -231,7 +231,7 @@ __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
 		creg = cxl_reg_block(pdev, &map);
 
 	port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
-	rc = cxl_add_dport(port, &pdev->dev, port_num, creg);
+	rc = cxl_add_dport(port, &pdev->dev, port_num, creg, true);
 	if (rc) {
 		ctx->error = rc;
 		return rc;
@@ -406,7 +406,7 @@ static int add_host_bridge_dport(struct device *match, void *arg)
 		dev_dbg(host, "No CHBS found for Host Bridge: %s\n",
 			dev_name(match));
 
-	rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs));
+	rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs), false);
 	if (rc) {
 		dev_err(host, "failed to add downstream port: %s\n",
 			dev_name(match));
drivers/cxl/core/bus.c +33 -1
index dffbd0ac64af..03394a3ae75f 100644
@@ -25,6 +25,8 @@
  */
 
 static DEFINE_IDA(cxl_port_ida);
+static LIST_HEAD(cxl_root_ports);
+static DECLARE_RWSEM(root_port_sem);
 
 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
 			    char *buf)
@@ -268,12 +270,31 @@ struct cxl_port *to_cxl_port(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(to_cxl_port);
 
+struct cxl_dport *cxl_get_root_dport(struct device *dev)
+{
+	struct cxl_dport *ret = NULL;
+	struct cxl_dport *dport;
+
+	down_read(&root_port_sem);
+	list_for_each_entry(dport, &cxl_root_ports, root_port_link) {
+		if (dport->dport == dev) {
+			ret = dport;
+			break;
+		}
+	}
+
+	up_read(&root_port_sem);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(cxl_get_root_dport);
+
 static void unregister_port(void *_port)
 {
 	struct cxl_port *port = _port;
 	struct cxl_dport *dport;
 
 	device_lock(&port->dev);
+	down_read(&root_port_sem);
 	list_for_each_entry(dport, &port->dports, list) {
 		char link_name[CXL_TARGET_STRLEN];
 
@@ -281,7 +302,10 @@ static void unregister_port(void *_port)
 			     dport->port_id) >= CXL_TARGET_STRLEN)
 			continue;
 		sysfs_remove_link(&port->dev.kobj, link_name);
+
+		list_del_init(&dport->root_port_link);
 	}
+	up_read(&root_port_sem);
 	device_unlock(&port->dev);
 	device_unregister(&port->dev);
 }
@@ -431,12 +455,13 @@ static int add_dport(struct cxl_port *port, struct cxl_dport *new)
  * @dport_dev: firmware or PCI device representing the dport
  * @port_id: identifier for this dport in a decoder's target list
  * @component_reg_phys: optional location of CXL component registers
+ * @root_port: is this a root port (hostbridge downstream)
  *
  * Note that all allocations and links are undone by cxl_port deletion
  * and release.
  */
 int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
-		  resource_size_t component_reg_phys)
+		  resource_size_t component_reg_phys, bool root_port)
 {
 	char link_name[CXL_TARGET_STRLEN];
 	struct cxl_dport *dport;
@@ -451,6 +476,7 @@ int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
 		return -ENOMEM;
 
 	INIT_LIST_HEAD(&dport->list);
+	INIT_LIST_HEAD(&dport->root_port_link);
 	dport->dport = get_device(dport_dev);
 	dport->port_id = port_id;
 	dport->component_reg_phys = component_reg_phys;
@@ -464,6 +490,12 @@ int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
 	if (rc)
 		goto err;
 
+	if (root_port) {
+		down_write(&root_port_sem);
+		list_add_tail(&dport->root_port_link, &cxl_root_ports);
+		up_write(&root_port_sem);
+	}
+
 	return 0;
 err:
 	cxl_dport_release(dport);
drivers/cxl/cxl.h +4 -1
index ad22caf9135c..d26d97e1098d 100644
@@ -291,6 +291,7 @@ struct cxl_port {
  * @component_reg_phys: downstream port component registers
  * @port: reference to cxl_port that contains this downstream port
  * @list: node for a cxl_port's list of cxl_dport instances
+ * @root_port_link: node for global list of root ports
  */
 struct cxl_dport {
 	struct device *dport;
@@ -298,6 +299,7 @@ struct cxl_dport {
 	resource_size_t component_reg_phys;
 	struct cxl_port *port;
 	struct list_head list;
+	struct list_head root_port_link;
 };
 
 struct cxl_port *to_cxl_port(struct device *dev);
@@ -306,7 +308,8 @@ struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
 				   struct cxl_port *parent_port);
 
 int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
-		  resource_size_t component_reg_phys);
+		  resource_size_t component_reg_phys, bool root_port);
+struct cxl_dport *cxl_get_root_dport(struct device *dev);
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
tools/testing/cxl/mock_acpi.c +2 -2
index 4c8a493ace56..ddefc4345f36 100644
@@ -57,7 +57,7 @@ static int match_add_root_port(struct pci_dev *pdev, void *data)
 
 	/* TODO walk DVSEC to find component register base */
 	port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
-	rc = cxl_add_dport(port, &pdev->dev, port_num, CXL_RESOURCE_NONE);
+	rc = cxl_add_dport(port, &pdev->dev, port_num, CXL_RESOURCE_NONE, true);
 	if (rc) {
 		dev_err(dev, "failed to add dport: %s (%d)\n",
 			dev_name(&pdev->dev), rc);
@@ -78,7 +78,7 @@ static int mock_add_root_port(struct platform_device *pdev, void *data)
 	struct device *dev = ctx->dev;
 	int rc;
 
-	rc = cxl_add_dport(port, &pdev->dev, pdev->id, CXL_RESOURCE_NONE);
+	rc = cxl_add_dport(port, &pdev->dev, pdev->id, CXL_RESOURCE_NONE, true);
 	if (rc) {
 		dev_err(dev, "failed to add dport: %s (%d)\n",
 			dev_name(&pdev->dev), rc);
Signature · 1 line
2.33.1
#59 lore raw

Re: [RFC PATCH v2 10/28] cxl/core: Store global list of root ports

Quoted 47 lines · Ben Widawsky

CXL root ports (the downstream port to a host bridge) are to be enumerated by a platform specific driver. In the case of ACPI compliant systems, this is like the cxl_acpi driver. Root ports are the first CXL spec defined component that can be "found" by that platform specific driver.

By storing a list of these root ports components in lower levels of the topology (switches and endpoints), have a mechanism to walk up their device hierarchy to find an enumerated root port. This will be necessary for region programming.

---
 drivers/cxl/acpi.c            |  4 ++--
 drivers/cxl/core/bus.c        | 34 +++++++++++++++++++++++++++++++++-
 drivers/cxl/cxl.h             |  5 ++++-
 tools/testing/cxl/mock_acpi.c |  4 ++--
 4 files changed, 41 insertions(+), 6 deletions(-)
drivers/cxl/acpi.c +2 -2
index 8cca0814dfb8..625c5d95b83f 100644
@@ -231,7 +231,7 @@ __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
                creg = cxl_reg_block(pdev, &map);
 
        port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
-       rc = cxl_add_dport(port, &pdev->dev, port_num, creg);
+       rc = cxl_add_dport(port, &pdev->dev, port_num, creg, true);
        if (rc) {
                ctx->error = rc;
                return rc;
@@ -406,7 +406,7 @@ static int add_host_bridge_dport(struct device *match, void *arg)
                dev_dbg(host, "No CHBS found for Host Bridge: %s\n",
                        dev_name(match));
 
-       rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs));
+       rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs), false);
        if (rc) {
                dev_err(host, "failed to add downstream port: %s\n",
                        dev_name(match));
drivers/cxl/core/bus.c +0 -0
index dffbd0ac64af..03394a3ae75f 100644

drivers/cxl/core/bus.c line 29

Quoted 5 lines
drivers/cxl/core/bus.c +2 -0
@@ -25,6 +25,8 @@
  */
 
 static DEFINE_IDA(cxl_port_ida);
+static LIST_HEAD(cxl_root_ports);
+static DECLARE_RWSEM(root_port_sem);

I don't see a need for this new list and lock...

Quoted 14 lines
drivers/cxl/core/bus.c +7 -0
 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
                            char *buf)
@@ -268,12 +270,31 @@ struct cxl_port *to_cxl_port(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(to_cxl_port);
 
+struct cxl_dport *cxl_get_root_dport(struct device *dev)
+{
+       struct cxl_dport *ret = NULL;
+       struct cxl_dport *dport;
+
+       down_read(&root_port_sem);
+       list_for_each_entry(dport, &cxl_root_ports, root_port_link) {

drivers/cxl/core/bus.c struct cxl_port *to_cxl_port(struct device *dev) · line 289

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+               if (dport->dport == dev) {
+                       ret = dport;
+                       break;
+               }
+       }
+
+       up_read(&root_port_sem);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(cxl_get_root_dport);

This can be done by walking the existing topology:

struct cxl_dport *cxl_get_root_dport(struct device *dev)

{
        struct device *host = get_cxl_topology_host();
        struct cxl_dport *dport, *found = NULL;
        struct cxl_port *port;
        struct device *root;

        if (!host)
                return NULL;

        root = device_find_child(host, &root, match_cxl_root_port);
        if (!root)
                goto out;
        device_lock(root);
        port = to_cxl_port(root);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dev) {
                        found = dport;
                        break;
                }
        device_unlock(root);
        put_device(root);

out:

        put_cxl_topology_host(host);
        return found;
}

It occurs to me after writing this that device_lock() for iterating dports can be offloaded to the topology rwsem.

Quoted 24 lines
drivers/cxl/core/bus.c +5 -0
+
 static void unregister_port(void *_port)
 {
        struct cxl_port *port = _port;
        struct cxl_dport *dport;
 
        device_lock(&port->dev);
+       down_read(&root_port_sem);
        list_for_each_entry(dport, &port->dports, list) {
                char link_name[CXL_TARGET_STRLEN];
 
@@ -281,7 +302,10 @@ static void unregister_port(void *_port)
                             dport->port_id) >= CXL_TARGET_STRLEN)
                        continue;
                sysfs_remove_link(&port->dev.kobj, link_name);
+
+               list_del_init(&dport->root_port_link);
        }
+       up_read(&root_port_sem);
        device_unlock(&port->dev);
        device_unregister(&port->dev);
 }
@@ -431,12 +455,13 @@ static int add_dport(struct cxl_port *port, struct cxl_dport *new)
  * @dport_dev: firmware or PCI device representing the dport

drivers/cxl/core/bus.c static int add_dport(struct cxl_port *port, struct cxl_dport *new) · line 464

Quoted 10 lines
drivers/cxl/core/bus.c +2 -1
  * @port_id: identifier for this dport in a decoder's target list
  * @component_reg_phys: optional location of CXL component registers
+ * @root_port: is this a root port (hostbridge downstream)
  *
  * Note that all allocations and links are undone by cxl_port deletion
  * and release.
  */
 int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
-                 resource_size_t component_reg_phys)
+                 resource_size_t component_reg_phys, bool root_port)

With the above implementation there's no need to add this last parameter.

#60 lore raw

Re: [RFC PATCH v2 10/28] cxl/core: Store global list of root ports

Quoted 106 lines · Dan Williams
Quoted 47 lines · Ben Widawsky

CXL root ports (the downstream port to a host bridge) are to be enumerated by a platform specific driver. In the case of ACPI compliant systems, this is like the cxl_acpi driver. Root ports are the first CXL spec defined component that can be "found" by that platform specific driver.

By storing a list of these root ports components in lower levels of the topology (switches and endpoints), have a mechanism to walk up their device hierarchy to find an enumerated root port. This will be necessary for region programming.

---
 drivers/cxl/acpi.c            |  4 ++--
 drivers/cxl/core/bus.c        | 34 +++++++++++++++++++++++++++++++++-
 drivers/cxl/cxl.h             |  5 ++++-
 tools/testing/cxl/mock_acpi.c |  4 ++--
 4 files changed, 41 insertions(+), 6 deletions(-)
drivers/cxl/acpi.c +2 -2
index 8cca0814dfb8..625c5d95b83f 100644
@@ -231,7 +231,7 @@ __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
                creg = cxl_reg_block(pdev, &map);
 
        port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
-       rc = cxl_add_dport(port, &pdev->dev, port_num, creg);
+       rc = cxl_add_dport(port, &pdev->dev, port_num, creg, true);
        if (rc) {
                ctx->error = rc;
                return rc;
@@ -406,7 +406,7 @@ static int add_host_bridge_dport(struct device *match, void *arg)
                dev_dbg(host, "No CHBS found for Host Bridge: %s\n",
                        dev_name(match));
 
-       rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs));
+       rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs), false);
        if (rc) {
                dev_err(host, "failed to add downstream port: %s\n",
                        dev_name(match));
drivers/cxl/core/bus.c +0 -0
index dffbd0ac64af..03394a3ae75f 100644

drivers/cxl/core/bus.c line 29

Quoted 5 lines
drivers/cxl/core/bus.c +2 -0
@@ -25,6 +25,8 @@
  */
 
 static DEFINE_IDA(cxl_port_ida);
+static LIST_HEAD(cxl_root_ports);
+static DECLARE_RWSEM(root_port_sem);

I don't see a need for this new list and lock...

Quoted 14 lines
drivers/cxl/core/bus.c +7 -0
 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
                            char *buf)
@@ -268,12 +270,31 @@ struct cxl_port *to_cxl_port(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(to_cxl_port);
 
+struct cxl_dport *cxl_get_root_dport(struct device *dev)
+{
+       struct cxl_dport *ret = NULL;
+       struct cxl_dport *dport;
+
+       down_read(&root_port_sem);
+       list_for_each_entry(dport, &cxl_root_ports, root_port_link) {

drivers/cxl/core/bus.c struct cxl_port *to_cxl_port(struct device *dev) · line 289

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+               if (dport->dport == dev) {
+                       ret = dport;
+                       break;
+               }
+       }
+
+       up_read(&root_port_sem);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(cxl_get_root_dport);

This can be done by walking the existing topology:

struct cxl_dport *cxl_get_root_dport(struct device *dev)

{
        struct device *host = get_cxl_topology_host();
        struct cxl_dport *dport, *found = NULL;
        struct cxl_port *port;
        struct device *root;

        if (!host)
                return NULL;

        root = device_find_child(host, &root, match_cxl_root_port);
        if (!root)
                goto out;
        device_lock(root);
        port = to_cxl_port(root);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dev) {
                        found = dport;
                        break;
                }
        device_unlock(root);
        put_device(root);
Quoted 9 lines

out:

        put_cxl_topology_host(host);
        return found;
}

It occurs to me after writing this that device_lock() for iterating dports can be offloaded to the topology rwsem.

When I originally wrote this function, I didn't have a pointer to the root host. Also, you said previously we'd remove the root host. What would you like me to do?

Quoted 36 lines
Quoted 24 lines
drivers/cxl/core/bus.c +5 -0
+
 static void unregister_port(void *_port)
 {
        struct cxl_port *port = _port;
        struct cxl_dport *dport;
 
        device_lock(&port->dev);
+       down_read(&root_port_sem);
        list_for_each_entry(dport, &port->dports, list) {
                char link_name[CXL_TARGET_STRLEN];
 
@@ -281,7 +302,10 @@ static void unregister_port(void *_port)
                             dport->port_id) >= CXL_TARGET_STRLEN)
                        continue;
                sysfs_remove_link(&port->dev.kobj, link_name);
+
+               list_del_init(&dport->root_port_link);
        }
+       up_read(&root_port_sem);
        device_unlock(&port->dev);
        device_unregister(&port->dev);
 }
@@ -431,12 +455,13 @@ static int add_dport(struct cxl_port *port, struct cxl_dport *new)
  * @dport_dev: firmware or PCI device representing the dport

drivers/cxl/core/bus.c static int add_dport(struct cxl_port *port, struct cxl_dport *new) · line 464

Quoted 10 lines
drivers/cxl/core/bus.c +2 -1
  * @port_id: identifier for this dport in a decoder's target list
  * @component_reg_phys: optional location of CXL component registers
+ * @root_port: is this a root port (hostbridge downstream)
  *
  * Note that all allocations and links are undone by cxl_port deletion
  * and release.
  */
 int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
-                 resource_size_t component_reg_phys)
+                 resource_size_t component_reg_phys, bool root_port)

With the above implementation there's no need to add this last parameter.

#61 lore raw

Re: [RFC PATCH v2 10/28] cxl/core: Store global list of root ports

Quoted 114 lines · Ben Widawsky
Quoted 112 lines · Dan Williams
Quoted 47 lines · Ben Widawsky

CXL root ports (the downstream port to a host bridge) are to be enumerated by a platform specific driver. In the case of ACPI compliant systems, this is like the cxl_acpi driver. Root ports are the first CXL spec defined component that can be "found" by that platform specific driver.

By storing a list of these root ports components in lower levels of the topology (switches and endpoints), have a mechanism to walk up their device hierarchy to find an enumerated root port. This will be necessary for region programming.

---
 drivers/cxl/acpi.c            |  4 ++--
 drivers/cxl/core/bus.c        | 34 +++++++++++++++++++++++++++++++++-
 drivers/cxl/cxl.h             |  5 ++++-
 tools/testing/cxl/mock_acpi.c |  4 ++--
 4 files changed, 41 insertions(+), 6 deletions(-)
drivers/cxl/acpi.c +2 -2
index 8cca0814dfb8..625c5d95b83f 100644
@@ -231,7 +231,7 @@ __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
                creg = cxl_reg_block(pdev, &map);
 
        port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
-       rc = cxl_add_dport(port, &pdev->dev, port_num, creg);
+       rc = cxl_add_dport(port, &pdev->dev, port_num, creg, true);
        if (rc) {
                ctx->error = rc;
                return rc;
@@ -406,7 +406,7 @@ static int add_host_bridge_dport(struct device *match, void *arg)
                dev_dbg(host, "No CHBS found for Host Bridge: %s\n",
                        dev_name(match));
 
-       rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs));
+       rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs), false);
        if (rc) {
                dev_err(host, "failed to add downstream port: %s\n",
                        dev_name(match));
drivers/cxl/core/bus.c +0 -0
index dffbd0ac64af..03394a3ae75f 100644

drivers/cxl/core/bus.c line 29

Quoted 5 lines
drivers/cxl/core/bus.c +2 -0
@@ -25,6 +25,8 @@
  */
 
 static DEFINE_IDA(cxl_port_ida);
+static LIST_HEAD(cxl_root_ports);
+static DECLARE_RWSEM(root_port_sem);

I don't see a need for this new list and lock...

Quoted 14 lines
drivers/cxl/core/bus.c +7 -0
 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
                            char *buf)
@@ -268,12 +270,31 @@ struct cxl_port *to_cxl_port(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(to_cxl_port);
 
+struct cxl_dport *cxl_get_root_dport(struct device *dev)
+{
+       struct cxl_dport *ret = NULL;
+       struct cxl_dport *dport;
+
+       down_read(&root_port_sem);
+       list_for_each_entry(dport, &cxl_root_ports, root_port_link) {

drivers/cxl/core/bus.c struct cxl_port *to_cxl_port(struct device *dev) · line 289

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+               if (dport->dport == dev) {
+                       ret = dport;
+                       break;
+               }
+       }
+
+       up_read(&root_port_sem);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(cxl_get_root_dport);

This can be done by walking the existing topology:

struct cxl_dport *cxl_get_root_dport(struct device *dev)

{
        struct device *host = get_cxl_topology_host();
        struct cxl_dport *dport, *found = NULL;
        struct cxl_port *port;
        struct device *root;

        if (!host)
                return NULL;

        root = device_find_child(host, &root, match_cxl_root_port);
        if (!root)
                goto out;
        device_lock(root);
        port = to_cxl_port(root);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dev) {
                        found = dport;
                        break;
                }
        device_unlock(root);
        put_device(root);

out:

        put_cxl_topology_host(host);
        return found;
}
Quoted 7 lines
Quoted 3 lines

It occurs to me after writing this that device_lock() for iterating dports can be offloaded to the topology rwsem.

When I originally wrote this function, I didn't have a pointer to the root host. Also, you said previously we'd remove the root host. What would you like me to do?

Root device retrieval is useful for these scans. However, the role of overall "devm host" can be removed with careful port-child devm registration.

#62 lore raw

RFC PATCH v2 11/28 cxl/acpi: Rescan bus at probe completion

In reply to Ben Widawsky

Ensure that devices being probed before cxl_acpi has completed will get a second chance.

CXL drivers are brought up through two enumerable, asynchronous mechanism. The leaf nodes in the CXL topology, endpoints, are enumerated via PCI headers. The root node's enumeration is platform specific. The current defacto mechanism for enumerating the root node is through the presence of an ACPI device, ACPI0017.

The primary job of a cxl_mem driver is to determine if CXL.mem traffic can be routed to/from the PCIe device that it is being probed. A prerequisite in this determination is that all CXL components in the path from root to leaf are capable of routing CXL.mem traffic. If the cxl_mem driver is probed before cxl_acpi is complete the driver will be unable to make this determination. To address this, cxl_acpi (or in the future, another platform specific driver) will rescan all devices to make sure the ordering is correct.

---
 drivers/cxl/acpi.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +19 -1
index 625c5d95b83f..1cc3a74c16bd 100644
@@ -451,6 +451,14 @@ static u32 cedt_instance(struct platform_device *pdev)
 	return U32_MAX;
 }
 
+static void bus_rescan(struct work_struct *work)
+{
+	if (bus_rescan_devices(&cxl_bus_type))
+		pr_err("Failed to rescan CXL bus\n");
+}
+
+static DECLARE_WORK(deferred_bus_rescan, bus_rescan);
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
 	int rc;
@@ -484,9 +492,19 @@ static int cxl_acpi_probe(struct platform_device *pdev)
 	if (rc)
 		goto out;
 
-	if (IS_ENABLED(CONFIG_CXL_PMEM))
+	if (IS_ENABLED(CONFIG_CXL_PMEM)) {
 		rc = device_for_each_child(&root_port->dev, root_port,
 					   add_root_nvdimm_bridge);
+		if (rc)
+			goto out;
+	}
+
+	/*
+	 * While ACPI is scanning hostbridge ports, switches and memory devices
+	 * may have been probed. Those devices will need to know whether the
+	 * hostbridge is CXL capable.
+	 */
+	schedule_work(&deferred_bus_rescan);
 
 out:
 	acpi_put_table(acpi_cedt);
Signature · 1 line
2.33.1
#63 lore raw

Re: [RFC PATCH v2 11/28] cxl/acpi: Rescan bus at probe completion

Quoted 3 lines · Ben Widawsky

Ensure that devices being probed before cxl_acpi has completed will get a second chance.

I think this is at the wrong level... more below.

Quoted 48 lines

CXL drivers are brought up through two enumerable, asynchronous mechanism. The leaf nodes in the CXL topology, endpoints, are enumerated via PCI headers. The root node's enumeration is platform specific. The current defacto mechanism for enumerating the root node is through the presence of an ACPI device, ACPI0017.

The primary job of a cxl_mem driver is to determine if CXL.mem traffic can be routed to/from the PCIe device that it is being probed. A prerequisite in this determination is that all CXL components in the path from root to leaf are capable of routing CXL.mem traffic. If the cxl_mem driver is probed before cxl_acpi is complete the driver will be unable to make this determination. To address this, cxl_acpi (or in the future, another platform specific driver) will rescan all devices to make sure the ordering is correct.

---
 drivers/cxl/acpi.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +9 -1
index 625c5d95b83f..1cc3a74c16bd 100644
@@ -451,6 +451,14 @@ static u32 cedt_instance(struct platform_device *pdev)
        return U32_MAX;
 }
 
+static void bus_rescan(struct work_struct *work)
+{
+       if (bus_rescan_devices(&cxl_bus_type))
+               pr_err("Failed to rescan CXL bus\n");
+}
+
+static DECLARE_WORK(deferred_bus_rescan, bus_rescan);
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
@@ -484,9 +492,19 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        if (rc)
                goto out;
 
-       if (IS_ENABLED(CONFIG_CXL_PMEM))
+       if (IS_ENABLED(CONFIG_CXL_PMEM)) {
                rc = device_for_each_child(&root_port->dev, root_port,
                                           add_root_nvdimm_bridge);

drivers/cxl/acpi.c static int cxl_acpi_probe(struct platform_device *pdev) · line 507

Quoted 10 lines
drivers/cxl/acpi.c +10 -0
+               if (rc)
+                       goto out;
+       }
+
+       /*
+        * While ACPI is scanning hostbridge ports, switches and memory devices
+        * may have been probed. Those devices will need to know whether the
+        * hostbridge is CXL capable.
+        */
+       schedule_work(&deferred_bus_rescan);

I think this belongs in port driver similar to the one in drivers/cxl/pmem.c, because any port online event might mean that a downstream port can now attach to the port driver. I prefer a local ordered workqueue via queue_work() rather than the global unordered workqueue primarily because it can be flushed without getting entangled with other random work in the system, but also because the ordered property is useful for comprehended hierarchical topology events.

In any event this is missing flushing to prevent races between .text removal at module_exit() and pending work.

#64 lore raw

Re: [RFC PATCH v2 11/28] cxl/acpi: Rescan bus at probe completion

Quoted 65 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Ensure that devices being probed before cxl_acpi has completed will get a second chance.

I think this is at the wrong level... more below.

Quoted 48 lines

CXL drivers are brought up through two enumerable, asynchronous mechanism. The leaf nodes in the CXL topology, endpoints, are enumerated via PCI headers. The root node's enumeration is platform specific. The current defacto mechanism for enumerating the root node is through the presence of an ACPI device, ACPI0017.

The primary job of a cxl_mem driver is to determine if CXL.mem traffic can be routed to/from the PCIe device that it is being probed. A prerequisite in this determination is that all CXL components in the path from root to leaf are capable of routing CXL.mem traffic. If the cxl_mem driver is probed before cxl_acpi is complete the driver will be unable to make this determination. To address this, cxl_acpi (or in the future, another platform specific driver) will rescan all devices to make sure the ordering is correct.

---
 drivers/cxl/acpi.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +9 -1
index 625c5d95b83f..1cc3a74c16bd 100644
@@ -451,6 +451,14 @@ static u32 cedt_instance(struct platform_device *pdev)
        return U32_MAX;
 }
 
+static void bus_rescan(struct work_struct *work)
+{
+       if (bus_rescan_devices(&cxl_bus_type))
+               pr_err("Failed to rescan CXL bus\n");
+}
+
+static DECLARE_WORK(deferred_bus_rescan, bus_rescan);
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
@@ -484,9 +492,19 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        if (rc)
                goto out;
 
-       if (IS_ENABLED(CONFIG_CXL_PMEM))
+       if (IS_ENABLED(CONFIG_CXL_PMEM)) {
                rc = device_for_each_child(&root_port->dev, root_port,
                                           add_root_nvdimm_bridge);

drivers/cxl/acpi.c static int cxl_acpi_probe(struct platform_device *pdev) · line 507

Quoted 10 lines
drivers/cxl/acpi.c +10 -0
+               if (rc)
+                       goto out;
+       }
+
+       /*
+        * While ACPI is scanning hostbridge ports, switches and memory devices
+        * may have been probed. Those devices will need to know whether the
+        * hostbridge is CXL capable.
+        */
+       schedule_work(&deferred_bus_rescan);
Quoted 8 lines

I think this belongs in port driver similar to the one in drivers/cxl/pmem.c, because any port online event might mean that a downstream port can now attach to the port driver. I prefer a local ordered workqueue via queue_work() rather than the global unordered workqueue primarily because it can be flushed without getting entangled with other random work in the system, but also because the ordered property is useful for comprehended hierarchical topology events.

Makes sense. I was trying to limit the number of rescans that took place and I believe I convinced myself that the current patch does work properly. However, architecturally it makes more sense for the port driver to own this and realistically I assume many rescans will coalesce.

Quoted 3 lines

In any event this is missing flushing to prevent races between .text removal at module_exit() and pending work.

#65 lore raw

Re: [RFC PATCH v2 11/28] cxl/acpi: Rescan bus at probe completion

Quoted 75 lines · Ben Widawsky
Quoted 65 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Ensure that devices being probed before cxl_acpi has completed will get a second chance.

I think this is at the wrong level... more below.

Quoted 48 lines

CXL drivers are brought up through two enumerable, asynchronous mechanism. The leaf nodes in the CXL topology, endpoints, are enumerated via PCI headers. The root node's enumeration is platform specific. The current defacto mechanism for enumerating the root node is through the presence of an ACPI device, ACPI0017.

The primary job of a cxl_mem driver is to determine if CXL.mem traffic can be routed to/from the PCIe device that it is being probed. A prerequisite in this determination is that all CXL components in the path from root to leaf are capable of routing CXL.mem traffic. If the cxl_mem driver is probed before cxl_acpi is complete the driver will be unable to make this determination. To address this, cxl_acpi (or in the future, another platform specific driver) will rescan all devices to make sure the ordering is correct.

---
 drivers/cxl/acpi.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +9 -1
index 625c5d95b83f..1cc3a74c16bd 100644
@@ -451,6 +451,14 @@ static u32 cedt_instance(struct platform_device *pdev)
        return U32_MAX;
 }
 
+static void bus_rescan(struct work_struct *work)
+{
+       if (bus_rescan_devices(&cxl_bus_type))
+               pr_err("Failed to rescan CXL bus\n");
+}
+
+static DECLARE_WORK(deferred_bus_rescan, bus_rescan);
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
@@ -484,9 +492,19 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        if (rc)
                goto out;
 
-       if (IS_ENABLED(CONFIG_CXL_PMEM))
+       if (IS_ENABLED(CONFIG_CXL_PMEM)) {
                rc = device_for_each_child(&root_port->dev, root_port,
                                           add_root_nvdimm_bridge);

drivers/cxl/acpi.c static int cxl_acpi_probe(struct platform_device *pdev) · line 507

Quoted 10 lines
drivers/cxl/acpi.c +10 -0
+               if (rc)
+                       goto out;
+       }
+
+       /*
+        * While ACPI is scanning hostbridge ports, switches and memory devices
+        * may have been probed. Those devices will need to know whether the
+        * hostbridge is CXL capable.
+        */
+       schedule_work(&deferred_bus_rescan);
Quoted 8 lines

I think this belongs in port driver similar to the one in drivers/cxl/pmem.c, because any port online event might mean that a downstream port can now attach to the port driver. I prefer a local ordered workqueue via queue_work() rather than the global unordered workqueue primarily because it can be flushed without getting entangled with other random work in the system, but also because the ordered property is useful for comprehended hierarchical topology events.

Quoted 5 lines

Makes sense. I was trying to limit the number of rescans that took place and I believe I convinced myself that the current patch does work properly. However, architecturally it makes more sense for the port driver to own this and realistically I assume many rescans will coalesce.

Oh. I assume you want this rolled into (cxl/port: Introduce a port driver), correct?

Quoted 4 lines
Quoted 3 lines

In any event this is missing flushing to prevent races between .text removal at module_exit() and pending work.

#66 lore raw

Re: [RFC PATCH v2 11/28] cxl/acpi: Rescan bus at probe completion

Quoted 77 lines · Ben Widawsky
Quoted 75 lines · Ben Widawsky
Quoted 73 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Ensure that devices being probed before cxl_acpi has completed will get a second chance.

I think this is at the wrong level... more below.

Quoted 48 lines

CXL drivers are brought up through two enumerable, asynchronous mechanism. The leaf nodes in the CXL topology, endpoints, are enumerated via PCI headers. The root node's enumeration is platform specific. The current defacto mechanism for enumerating the root node is through the presence of an ACPI device, ACPI0017.

The primary job of a cxl_mem driver is to determine if CXL.mem traffic can be routed to/from the PCIe device that it is being probed. A prerequisite in this determination is that all CXL components in the path from root to leaf are capable of routing CXL.mem traffic. If the cxl_mem driver is probed before cxl_acpi is complete the driver will be unable to make this determination. To address this, cxl_acpi (or in the future, another platform specific driver) will rescan all devices to make sure the ordering is correct.

---
 drivers/cxl/acpi.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/cxl/acpi.c +9 -1
index 625c5d95b83f..1cc3a74c16bd 100644
@@ -451,6 +451,14 @@ static u32 cedt_instance(struct platform_device *pdev)
        return U32_MAX;
 }
 
+static void bus_rescan(struct work_struct *work)
+{
+       if (bus_rescan_devices(&cxl_bus_type))
+               pr_err("Failed to rescan CXL bus\n");
+}
+
+static DECLARE_WORK(deferred_bus_rescan, bus_rescan);
+
 static int cxl_acpi_probe(struct platform_device *pdev)
 {
        int rc;
@@ -484,9 +492,19 @@ static int cxl_acpi_probe(struct platform_device *pdev)
        if (rc)
                goto out;
 
-       if (IS_ENABLED(CONFIG_CXL_PMEM))
+       if (IS_ENABLED(CONFIG_CXL_PMEM)) {
                rc = device_for_each_child(&root_port->dev, root_port,
                                           add_root_nvdimm_bridge);

drivers/cxl/acpi.c static int cxl_acpi_probe(struct platform_device *pdev) · line 507

Quoted 10 lines
drivers/cxl/acpi.c +10 -0
+               if (rc)
+                       goto out;
+       }
+
+       /*
+        * While ACPI is scanning hostbridge ports, switches and memory devices
+        * may have been probed. Those devices will need to know whether the
+        * hostbridge is CXL capable.
+        */
+       schedule_work(&deferred_bus_rescan);

I think this belongs in port driver similar to the one in drivers/cxl/pmem.c, because any port online event might mean that a downstream port can now attach to the port driver. I prefer a local ordered workqueue via queue_work() rather than the global unordered workqueue primarily because it can be flushed without getting entangled with other random work in the system, but also because the ordered property is useful for comprehended hierarchical topology events.

Quoted 8 lines
Quoted 5 lines

Makes sense. I was trying to limit the number of rescans that took place and I believe I convinced myself that the current patch does work properly. However, architecturally it makes more sense for the port driver to own this and realistically I assume many rescans will coalesce.

Oh. I assume you want this rolled into (cxl/port: Introduce a port driver), correct?

Yeah, otherwise the port driver is broken, right?

#67 lore raw

RFC PATCH v2 12/28 cxl/core: Store component register base for memdevs

In reply to Ben Widawsky

Bake component registers into the memdev creation API in order to be able to use them as part of driver probing.

Component register base addresses are obtained through PCI mechanisms. As such it makes most sense for the cxl_pci driver to obtain that address. In order to reuse the port driver for enumerating decoder resources for an endpoint, it is desirable to be able to add the endpoint as a port. Unfortunately, by the time an endpoint driver would run, it no longer has any concept of the underlying PCI device (this is done intentionally to provide separation between drivers).

---
 drivers/cxl/core/memdev.c    |  5 +++--
 drivers/cxl/cxlmem.h         |  5 ++++-
 drivers/cxl/pci.c            | 17 ++++++++++++++++-
 tools/testing/cxl/test/mem.c |  3 ++-
 4 files changed, 25 insertions(+), 5 deletions(-)
drivers/cxl/core/memdev.c +3 -2
index bf1b04d00ff4..15762c16d83f 100644
@@ -276,8 +276,8 @@ static const struct file_operations cxl_memdev_fops = {
 	.llseek = noop_llseek,
 };
 
-struct cxl_memdev *
-devm_cxl_add_memdev(struct cxl_mem *cxlm)
+struct cxl_memdev *devm_cxl_add_memdev(struct cxl_mem *cxlm,
+				       resource_size_t component_reg_phys)
 {
 	struct cxl_memdev *cxlmd;
 	struct device *dev;
@@ -298,6 +298,7 @@ devm_cxl_add_memdev(struct cxl_mem *cxlm)
 	 * needed as this is ordered with cdev_add() publishing the device.
 	 */
 	cxlmd->cxlm = cxlm;
+	cxlmd->creg_base = component_reg_phys;
 
 	cdev = &cxlmd->cdev;
 	rc = cdev_device_add(cdev, dev);
drivers/cxl/cxlmem.h +4 -1
index c4f450ad434d..62fe8e2c59e4 100644
@@ -35,12 +35,14 @@
  * @cdev: char dev core object for ioctl operations
  * @cxlm: pointer to the parent device driver data
  * @id: id number of this memdev instance.
+ * @creg_base: register base of component registers
  */
 struct cxl_memdev {
 	struct device dev;
 	struct cdev cdev;
 	struct cxl_mem *cxlm;
 	int id;
+	resource_size_t creg_base;
 };
 
 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
@@ -48,7 +50,8 @@ static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
 	return container_of(dev, struct cxl_memdev, dev);
 }
 
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_mem *cxlm);
+struct cxl_memdev *devm_cxl_add_memdev(struct cxl_mem *cxlm,
+				       resource_size_t component_reg_phys);
 
 /**
  * struct cxl_mbox_cmd - A command to be submitted to hardware.
drivers/cxl/pci.c +16 -1
index d1adc759d051..96a312ed8269 100644
@@ -421,6 +421,7 @@ static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
 
 static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
+	resource_size_t creg = CXL_RESOURCE_NONE;
 	struct cxl_register_map map;
 	struct cxl_memdev *cxlmd;
 	struct cxl_mem *cxlm;
@@ -465,7 +466,21 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (rc)
 		return rc;
 
-	cxlmd = devm_cxl_add_memdev(cxlm);
+	/*
+	 * If the component registers can't be found, the cxl_pci driver may
+	 * still be useful for management functions so don't return an error.
+	 *
+	 * XXX: Creating the device is going to kick of the cxl_mem probing.
+	 * That probe requires the component registers. Therefore, the register
+	 * block must always be found first.
+	 */
+	rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+	if (rc)
+		dev_warn(&cxlmd->dev, "No component registers (%d)\n", rc);
+	else
+		creg = cxl_reg_block(pdev, &map);
+
+	cxlmd = devm_cxl_add_memdev(cxlm, creg);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
tools/testing/cxl/test/mem.c +2 -1
index 12a8437a9ca0..471fc7fb5418 100644
@@ -227,7 +227,8 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
 	if (rc)
 		return rc;
 
-	cxlmd = devm_cxl_add_memdev(cxlm);
+	/* TODO: mock component registers, or... */
+	cxlmd = devm_cxl_add_memdev(cxlm, CXL_RESOURCE_NONE);
 	if (IS_ERR(cxlmd))
 		return PTR_ERR(cxlmd);
 
Signature · 1 line
2.33.1
#68 lore raw

Re: [RFC PATCH v2 12/28] cxl/core: Store component register base for memdevs

Quoted 11 lines · Ben Widawsky

Bake component registers into the memdev creation API in order to be able to use them as part of driver probing.

Component register base addresses are obtained through PCI mechanisms. As such it makes most sense for the cxl_pci driver to obtain that address. In order to reuse the port driver for enumerating decoder resources for an endpoint, it is desirable to be able to add the endpoint as a port. Unfortunately, by the time an endpoint driver would run, it no longer has any concept of the underlying PCI device (this is done intentionally to provide separation between drivers).

Changelog looks good.

Quoted 25 lines
---
 drivers/cxl/core/memdev.c    |  5 +++--
 drivers/cxl/cxlmem.h         |  5 ++++-
 drivers/cxl/pci.c            | 17 ++++++++++++++++-
 tools/testing/cxl/test/mem.c |  3 ++-
 4 files changed, 25 insertions(+), 5 deletions(-)
drivers/cxl/core/memdev.c +2 -2
index bf1b04d00ff4..15762c16d83f 100644
@@ -276,8 +276,8 @@ static const struct file_operations cxl_memdev_fops = {
        .llseek = noop_llseek,
 };
 
-struct cxl_memdev *
-devm_cxl_add_memdev(struct cxl_mem *cxlm)
+struct cxl_memdev *devm_cxl_add_memdev(struct cxl_mem *cxlm,
+                                      resource_size_t component_reg_phys)
 {
        struct cxl_memdev *cxlmd;
        struct device *dev;

drivers/cxl/core/memdev.c devm_cxl_add_memdev(struct cxl_mem *cxlm) · line 301

Quoted 4 lines
drivers/cxl/core/memdev.c +1 -0
@@ -298,6 +298,7 @@ devm_cxl_add_memdev(struct cxl_mem *cxlm)
         * needed as this is ordered with cdev_add() publishing the device.
         */
        cxlmd->cxlm = cxlm;
+       cxlmd->creg_base = component_reg_phys;

Let's pick one style for both, either rename component_reg_phys to creg_base, or creg_base to component_reg_phys. My preference is component_reg_phys, but I won't put up much of a fight against creg_base.

Quoted 45 lines
drivers/cxl/core/memdev.c +0 -0
        cdev = &cxlmd->cdev;
        rc = cdev_device_add(cdev, dev);
drivers/cxl/cxlmem.h +4 -1
index c4f450ad434d..62fe8e2c59e4 100644
@@ -35,12 +35,14 @@
  * @cdev: char dev core object for ioctl operations
  * @cxlm: pointer to the parent device driver data
  * @id: id number of this memdev instance.
+ * @creg_base: register base of component registers
  */
 struct cxl_memdev {
        struct device dev;
        struct cdev cdev;
        struct cxl_mem *cxlm;
        int id;
+       resource_size_t creg_base;
 };
 
 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
@@ -48,7 +50,8 @@ static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
        return container_of(dev, struct cxl_memdev, dev);
 }
 
-struct cxl_memdev *devm_cxl_add_memdev(struct cxl_mem *cxlm);
+struct cxl_memdev *devm_cxl_add_memdev(struct cxl_mem *cxlm,
+                                      resource_size_t component_reg_phys);
 
 /**
  * struct cxl_mbox_cmd - A command to be submitted to hardware.
drivers/cxl/pci.c +1 -0
index d1adc759d051..96a312ed8269 100644
@@ -421,6 +421,7 @@ static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
 
 static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
+       resource_size_t creg = CXL_RESOURCE_NONE;
        struct cxl_register_map map;
        struct cxl_memdev *cxlmd;
        struct cxl_mem *cxlm;

drivers/cxl/pci.c static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 471

Quoted 7 lines
drivers/cxl/pci.c +3 -1
@@ -465,7 +466,21 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        if (rc)
                return rc;
 
-       cxlmd = devm_cxl_add_memdev(cxlm);
+       /*
+        * If the component registers can't be found, the cxl_pci driver may
+        * still be useful for management functions so don't return an error.

This comment makes sense...

drivers/cxl/pci.c static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 476

Quoted 5 lines
drivers/cxl/pci.c +5 -0
+        *
+        * XXX: Creating the device is going to kick of the cxl_mem probing.
+        * That probe requires the component registers. Therefore, the register
+        * block must always be found first.
+        */

..., but I don't understand the point of this comment. Given that devm_cxl_add_memdev() takes the base address as an argument it's already clear that the component registers need to be found before devm_cxl_add_memdev().

Quoted 15 lines
drivers/cxl/pci.c +7 -0
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+       if (rc)
+               dev_warn(&cxlmd->dev, "No component registers (%d)\n", rc);
+       else
+               creg = cxl_reg_block(pdev, &map);
+
+       cxlmd = devm_cxl_add_memdev(cxlm, creg);
        if (IS_ERR(cxlmd))
                return PTR_ERR(cxlmd);
 
tools/testing/cxl/test/mem.c +0 -0
index 12a8437a9ca0..471fc7fb5418 100644

tools/testing/cxl/test/mem.c static int cxl_mock_mem_probe(struct platform_device *pdev) · line 230

Quoted 5 lines
tools/testing/cxl/test/mem.c +1 -1
@@ -227,7 +227,8 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
        if (rc)
                return rc;
 
-       cxlmd = devm_cxl_add_memdev(cxlm);
+       /* TODO: mock component registers, or... */

How about:

/* cxl_test does not emulate registers, any memdev operations that imply component register access will be mocked at the memdev operations interface */

Quoted 7 lines
tools/testing/cxl/test/mem.c +1 -1
+       cxlmd = devm_cxl_add_memdev(cxlm, CXL_RESOURCE_NONE);
        if (IS_ERR(cxlmd))
                return PTR_ERR(cxlmd);
 
--

2.33.1

#69 lore raw

Re: [RFC PATCH v2 12/28] cxl/core: Store component register base for memdevs

Quoted 2 lines · Dan Williams
Quoted 1 line · Ben Widawsky

[snip]

Quoted 13 lines

drivers/cxl/pci.c static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 471

Quoted 4 lines
drivers/cxl/pci.c +3 -1
-       cxlmd = devm_cxl_add_memdev(cxlm);
+       /*
+        * If the component registers can't be found, the cxl_pci driver may
+        * still be useful for management functions so don't return an error.

This comment makes sense...

drivers/cxl/pci.c static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 476

Quoted 5 lines
drivers/cxl/pci.c +5 -0
+        *
+        * XXX: Creating the device is going to kick of the cxl_mem probing.
+        * That probe requires the component registers. Therefore, the register
+        * block must always be found first.
+        */
Quoted 5 lines

..., but I don't understand the point of this comment. Given that devm_cxl_add_memdev() takes the base address as an argument it's already clear that the component registers need to be found before devm_cxl_add_memdev().

That comment was created before devm_cxl_add_memdev() took the base. Indeed the comment no longer makes sense.

Quoted 18 lines
Quoted 18 lines
drivers/cxl/pci.c +7 -0
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+       if (rc)
+               dev_warn(&cxlmd->dev, "No component registers (%d)\n", rc);
+       else
+               creg = cxl_reg_block(pdev, &map);
+
+       cxlmd = devm_cxl_add_memdev(cxlm, creg);
        if (IS_ERR(cxlmd))
                return PTR_ERR(cxlmd);
tools/testing/cxl/test/mem.c +0 -0
index 12a8437a9ca0..471fc7fb5418 100644
@@ -227,7 +227,8 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
        if (rc)
                return rc;
 
Quoted 9 lines

tools/testing/cxl/test/mem.c static int cxl_mock_mem_probe(struct platform_device *pdev) · line 230

Quoted 2 lines
tools/testing/cxl/test/mem.c +1 -1
-       cxlmd = devm_cxl_add_memdev(cxlm);
+       /* TODO: mock component registers, or... */

How about:

/* cxl_test does not emulate registers, any memdev operations that imply component register access will be mocked at the memdev operations interface */

Sounds good to me.

Quoted 7 lines
Quoted 7 lines
tools/testing/cxl/test/mem.c +1 -1
+       cxlmd = devm_cxl_add_memdev(cxlm, CXL_RESOURCE_NONE);
        if (IS_ERR(cxlmd))
                return PTR_ERR(cxlmd);
 
--

2.33.1

#70 lore raw

RFC PATCH v2 13/28 cxl: Flesh out register names

In reply to Ben Widawsky

Get a better naming scheme in place for upcoming additions. To solidify the schema, add all the DVSEC identifiers to start with.

---
See:
https://lore.kernel.org/linux-pci/[email protected]/

---
 drivers/cxl/core/regs.c | 14 ++++++++------
 drivers/cxl/pci.h       | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 38 insertions(+), 14 deletions(-)
drivers/cxl/core/regs.c +8 -6
index c8ab8880b81b..b837196fbf39 100644
@@ -253,9 +253,11 @@ static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
 				struct cxl_register_map *map)
 {
 	map->block_offset =
-		((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
-	map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
-	map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
+		((u64)reg_hi << 32) |
+		(reg_lo & DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK);
+	map->barno = FIELD_GET(DVSEC_REGISTER_LOCATOR_BIR_MASK, reg_lo);
+	map->reg_type =
+		FIELD_GET(DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK, reg_lo);
 }
 
 /**
@@ -276,15 +278,15 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
 	int regloc, i;
 
 	regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
-					   PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+					   CXL_DVSEC_REGISTER_LOCATOR);
 	if (!regloc)
 		return -ENXIO;
 
 	pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
 	regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
 
-	regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
-	regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+	regloc += DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET;
+	regblocks = (regloc_size - DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET) / 8;
 
 	for (i = 0; i < regblocks; i++, regloc += 8) {
 		u32 reg_lo, reg_hi;
drivers/cxl/pci.h +30 -8
index 12fdcb1b14e5..fe2898b17736 100644
@@ -7,17 +7,36 @@
 
 /*
  * See section 8.1 Configuration Space Registers in the CXL 2.0
- * Specification
+ * Specification. Names are taken straight from the specification with "CXL" and
+ * "DVSEC" redundancies removed.
  */
 #define PCI_DVSEC_HEADER1_LENGTH_MASK	GENMASK(31, 20)
 #define PCI_DVSEC_VENDOR_ID_CXL		0x1E98
-#define PCI_DVSEC_ID_CXL		0x0
 
-#define PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID	0x8
-#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET	0xC
+/* 8.1.3: PCIe DVSEC for CXL Device */
+#define CXL_DVSEC_PCIE_DEVICE					0
 
-/* BAR Indicator Register (BIR) */
-#define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
+/* 8.1.4: Non-CXL Function Map DVSEC */
+#define CXL_DVSEC_FUNCTION_MAP					2
+
+/* 8.1.5: CXL 2.0 Extensions DVSEC for Ports */
+#define CXL_DVSEC_PORT_EXTENSIONS				3
+
+/* 8.1.6: GPF DVSEC for CXL Port */
+#define CXL_DVSEC_PORT_GPF					4
+
+/* 8.1.7: GPF DVSEC for CXL Device */
+#define CXL_DVSEC_DEVICE_GPF					5
+
+/* 8.1.8: PCIe DVSEC for Flex Bus Port */
+#define CXL_DVSEC_PCIE_FLEXBUS_PORT				7
+
+/* 8.1.9: Register Locator DVSEC */
+#define CXL_DVSEC_REGISTER_LOCATOR				8
+#define   DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET			0xC
+#define     DVSEC_REGISTER_LOCATOR_BIR_MASK			GENMASK(2, 0)
+#define	    DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK	GENMASK(15, 8)
+#define     DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK	GENMASK(31, 16)
 
 /* Register Block Identifier (RBI) */
 enum cxl_regloc_type {
@@ -28,8 +47,11 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_TYPES
 };
 
-#define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
-#define CXL_REGLOC_ADDR_MASK GENMASK(31, 16)
+/* 8.1.10: MLD DVSEC */
+#define CXL_DVSEC_MLD						9
+
+/* 14.16.1 CXL Device Test Capability Advertisement */
+#define CXL_DVSEC_PCIE_TEST_CAPABILITY				10
 
 #define cxl_reg_block(pdev, map)                                               \
 	((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
Signature · 1 line
2.33.1
#71 lore raw

Re: [RFC PATCH v2 13/28] cxl: Flesh out register names

Quoted 3 lines · Ben Widawsky

Get a better naming scheme in place for upcoming additions. To solidify the schema, add all the DVSEC identifiers to start with.

The title and this changelog don't give anything of substance to review the patch.

This also looks like a rename and addition of more definitions. The rename has one rationale, the additional definitions have a different one, so split those into 2 patches, or fold the additions into the patch that uses them.

Quoted 6 lines

---
See:
https://lore.kernel.org/linux-pci/[email protected]/

Perhaps summarize this above, it's not clear what's relevant from that thread to this patch.

Quoted 107 lines
---
 drivers/cxl/core/regs.c | 14 ++++++++------
 drivers/cxl/pci.h       | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 38 insertions(+), 14 deletions(-)
drivers/cxl/core/regs.c +8 -6
index c8ab8880b81b..b837196fbf39 100644
@@ -253,9 +253,11 @@ static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
                                struct cxl_register_map *map)
 {
        map->block_offset =
-               ((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
-       map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
-       map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
+               ((u64)reg_hi << 32) |
+               (reg_lo & DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK);
+       map->barno = FIELD_GET(DVSEC_REGISTER_LOCATOR_BIR_MASK, reg_lo);
+       map->reg_type =
+               FIELD_GET(DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK, reg_lo);
 }
 
 /**
@@ -276,15 +278,15 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
        int regloc, i;
 
        regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
-                                          PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+                                          CXL_DVSEC_REGISTER_LOCATOR);
        if (!regloc)
                return -ENXIO;
 
        pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
        regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
 
-       regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
-       regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+       regloc += DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET;
+       regblocks = (regloc_size - DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET) / 8;
 
        for (i = 0; i < regblocks; i++, regloc += 8) {
                u32 reg_lo, reg_hi;
drivers/cxl/pci.h +30 -9
index 12fdcb1b14e5..fe2898b17736 100644
@@ -7,17 +7,36 @@
 
 /*
  * See section 8.1 Configuration Space Registers in the CXL 2.0
- * Specification
+ * Specification. Names are taken straight from the specification with "CXL" and
+ * "DVSEC" redundancies removed.
  */
 #define PCI_DVSEC_HEADER1_LENGTH_MASK  GENMASK(31, 20)
 #define PCI_DVSEC_VENDOR_ID_CXL                0x1E98
-#define PCI_DVSEC_ID_CXL               0x0
 
-#define PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID       0x8
-#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET  0xC
+/* 8.1.3: PCIe DVSEC for CXL Device */
+#define CXL_DVSEC_PCIE_DEVICE                                  0
 
-/* BAR Indicator Register (BIR) */
-#define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
+/* 8.1.4: Non-CXL Function Map DVSEC */
+#define CXL_DVSEC_FUNCTION_MAP                                 2
+
+/* 8.1.5: CXL 2.0 Extensions DVSEC for Ports */
+#define CXL_DVSEC_PORT_EXTENSIONS                              3
+
+/* 8.1.6: GPF DVSEC for CXL Port */
+#define CXL_DVSEC_PORT_GPF                                     4
+
+/* 8.1.7: GPF DVSEC for CXL Device */
+#define CXL_DVSEC_DEVICE_GPF                                   5
+
+/* 8.1.8: PCIe DVSEC for Flex Bus Port */
+#define CXL_DVSEC_PCIE_FLEXBUS_PORT                            7
+
+/* 8.1.9: Register Locator DVSEC */
+#define CXL_DVSEC_REGISTER_LOCATOR                             8
+#define   DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET                 0xC
+#define     DVSEC_REGISTER_LOCATOR_BIR_MASK                    GENMASK(2, 0)
+#define            DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK        GENMASK(15, 8)
+#define     DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK       GENMASK(31, 16)
 
 /* Register Block Identifier (RBI) */
 enum cxl_regloc_type {
@@ -28,8 +47,11 @@ enum cxl_regloc_type {
        CXL_REGLOC_RBI_TYPES
 };
 
-#define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
-#define CXL_REGLOC_ADDR_MASK GENMASK(31, 16)
+/* 8.1.10: MLD DVSEC */
+#define CXL_DVSEC_MLD                                          9
+
+/* 14.16.1 CXL Device Test Capability Advertisement */
+#define CXL_DVSEC_PCIE_TEST_CAPABILITY                         10
 
 #define cxl_reg_block(pdev, map)                                               \
        ((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
--

2.33.1

#72 lore raw

Re: [RFC PATCH v2 13/28] cxl: Flesh out register names

Quoted 5 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Get a better naming scheme in place for upcoming additions. To solidify the schema, add all the DVSEC identifiers to start with.

Quoted 8 lines

The title and this changelog don't give anything of substance to review the patch.

This also looks like a rename and addition of more definitions. The rename has one rationale, the additional definitions have a different one, so split those into 2 patches, or fold the additions into the patch that uses them.

I added more than necessary as a means to codify the naming scheme [as stated]. Many of them are not used. I can split this patch into two, though I personally don't find it offensive to do it as one. Before I do that, I'd like to know though if you're going to reject the patch if I'm not actually using all of the defines later on.

Quoted 117 lines
Quoted 6 lines

---
See:
https://lore.kernel.org/linux-pci/[email protected]/

Perhaps summarize this above, it's not clear what's relevant from that thread to this patch.

Quoted 107 lines
---
 drivers/cxl/core/regs.c | 14 ++++++++------
 drivers/cxl/pci.h       | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 38 insertions(+), 14 deletions(-)
drivers/cxl/core/regs.c +8 -6
index c8ab8880b81b..b837196fbf39 100644
@@ -253,9 +253,11 @@ static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
                                struct cxl_register_map *map)
 {
        map->block_offset =
-               ((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
-       map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
-       map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
+               ((u64)reg_hi << 32) |
+               (reg_lo & DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK);
+       map->barno = FIELD_GET(DVSEC_REGISTER_LOCATOR_BIR_MASK, reg_lo);
+       map->reg_type =
+               FIELD_GET(DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK, reg_lo);
 }
 
 /**
@@ -276,15 +278,15 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
        int regloc, i;
 
        regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
-                                          PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+                                          CXL_DVSEC_REGISTER_LOCATOR);
        if (!regloc)
                return -ENXIO;
 
        pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
        regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
 
-       regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
-       regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+       regloc += DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET;
+       regblocks = (regloc_size - DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET) / 8;
 
        for (i = 0; i < regblocks; i++, regloc += 8) {
                u32 reg_lo, reg_hi;
drivers/cxl/pci.h +30 -9
index 12fdcb1b14e5..fe2898b17736 100644
@@ -7,17 +7,36 @@
 
 /*
  * See section 8.1 Configuration Space Registers in the CXL 2.0
- * Specification
+ * Specification. Names are taken straight from the specification with "CXL" and
+ * "DVSEC" redundancies removed.
  */
 #define PCI_DVSEC_HEADER1_LENGTH_MASK  GENMASK(31, 20)
 #define PCI_DVSEC_VENDOR_ID_CXL                0x1E98
-#define PCI_DVSEC_ID_CXL               0x0
 
-#define PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID       0x8
-#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET  0xC
+/* 8.1.3: PCIe DVSEC for CXL Device */
+#define CXL_DVSEC_PCIE_DEVICE                                  0
 
-/* BAR Indicator Register (BIR) */
-#define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
+/* 8.1.4: Non-CXL Function Map DVSEC */
+#define CXL_DVSEC_FUNCTION_MAP                                 2
+
+/* 8.1.5: CXL 2.0 Extensions DVSEC for Ports */
+#define CXL_DVSEC_PORT_EXTENSIONS                              3
+
+/* 8.1.6: GPF DVSEC for CXL Port */
+#define CXL_DVSEC_PORT_GPF                                     4
+
+/* 8.1.7: GPF DVSEC for CXL Device */
+#define CXL_DVSEC_DEVICE_GPF                                   5
+
+/* 8.1.8: PCIe DVSEC for Flex Bus Port */
+#define CXL_DVSEC_PCIE_FLEXBUS_PORT                            7
+
+/* 8.1.9: Register Locator DVSEC */
+#define CXL_DVSEC_REGISTER_LOCATOR                             8
+#define   DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET                 0xC
+#define     DVSEC_REGISTER_LOCATOR_BIR_MASK                    GENMASK(2, 0)
+#define            DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK        GENMASK(15, 8)
+#define     DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK       GENMASK(31, 16)
 
 /* Register Block Identifier (RBI) */
 enum cxl_regloc_type {
@@ -28,8 +47,11 @@ enum cxl_regloc_type {
        CXL_REGLOC_RBI_TYPES
 };
 
-#define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
-#define CXL_REGLOC_ADDR_MASK GENMASK(31, 16)
+/* 8.1.10: MLD DVSEC */
+#define CXL_DVSEC_MLD                                          9
+
+/* 14.16.1 CXL Device Test Capability Advertisement */
+#define CXL_DVSEC_PCIE_TEST_CAPABILITY                         10
 
 #define cxl_reg_block(pdev, map)                                               \
        ((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
--

2.33.1

#73 lore raw

Re: [RFC PATCH v2 13/28] cxl: Flesh out register names

Quoted 16 lines · Ben Widawsky
Quoted 5 lines · Dan Williams
Quoted 3 lines · Ben Widawsky

Get a better naming scheme in place for upcoming additions. To solidify the schema, add all the DVSEC identifiers to start with.

Quoted 8 lines

The title and this changelog don't give anything of substance to review the patch.

This also looks like a rename and addition of more definitions. The rename has one rationale, the additional definitions have a different one, so split those into 2 patches, or fold the additions into the patch that uses them.

Quoted 6 lines

I added more than necessary as a means to codify the naming scheme [as stated]. Many of them are not used. I can split this patch into two, though I personally don't find it offensive to do it as one. Before I do that, I'd like to know though if you're going to reject the patch if I'm not actually using all of the defines later on.

They don't all need to be used, but a rationale for adding them is the request.

#74 lore raw

Re: [RFC PATCH v2 13/28] cxl: Flesh out register names

In reply to Ben Widawsky
Quoted 82 lines · On Fri, 22 Oct 2021 11:36:54 -0700 Ben Widawsky <[email protected]> wrote:

Get a better naming scheme in place for upcoming additions. To solidify the schema, add all the DVSEC identifiers to start with.

---
See:
https://lore.kernel.org/linux-pci/[email protected]/

---
 drivers/cxl/core/regs.c | 14 ++++++++------
 drivers/cxl/pci.h       | 38 ++++++++++++++++++++++++++++++--------
 2 files changed, 38 insertions(+), 14 deletions(-)
drivers/cxl/core/regs.c +8 -6
index c8ab8880b81b..b837196fbf39 100644
@@ -253,9 +253,11 @@ static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
 				struct cxl_register_map *map)
 {
 	map->block_offset =
-		((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
-	map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
-	map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
+		((u64)reg_hi << 32) |
+		(reg_lo & DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK);
+	map->barno = FIELD_GET(DVSEC_REGISTER_LOCATOR_BIR_MASK, reg_lo);
+	map->reg_type =
+		FIELD_GET(DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK, reg_lo);
 }
 
 /**
@@ -276,15 +278,15 @@ int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
 	int regloc, i;
 
 	regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
-					   PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
+					   CXL_DVSEC_REGISTER_LOCATOR);
 	if (!regloc)
 		return -ENXIO;
 
 	pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
 	regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
 
-	regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
-	regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
+	regloc += DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET;
+	regblocks = (regloc_size - DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET) / 8;
 
 	for (i = 0; i < regblocks; i++, regloc += 8) {
 		u32 reg_lo, reg_hi;
drivers/cxl/pci.h +12 -6
index 12fdcb1b14e5..fe2898b17736 100644
@@ -7,17 +7,36 @@
 
 /*
  * See section 8.1 Configuration Space Registers in the CXL 2.0
- * Specification
+ * Specification. Names are taken straight from the specification with "CXL" and
+ * "DVSEC" redundancies removed.
  */
 #define PCI_DVSEC_HEADER1_LENGTH_MASK	GENMASK(31, 20)
 #define PCI_DVSEC_VENDOR_ID_CXL		0x1E98
-#define PCI_DVSEC_ID_CXL		0x0
 
-#define PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID	0x8
-#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET	0xC
+/* 8.1.3: PCIe DVSEC for CXL Device */
+#define CXL_DVSEC_PCIE_DEVICE					0
 
-/* BAR Indicator Register (BIR) */
-#define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
+/* 8.1.4: Non-CXL Function Map DVSEC */
+#define CXL_DVSEC_FUNCTION_MAP					2
+
+/* 8.1.5: CXL 2.0 Extensions DVSEC for Ports */
+#define CXL_DVSEC_PORT_EXTENSIONS				3
+
+/* 8.1.6: GPF DVSEC for CXL Port */
+#define CXL_DVSEC_PORT_GPF					4

drivers/cxl/pci.h line 36

Quoted 10 lines
drivers/cxl/pci.h +10 -0
+
+/* 8.1.7: GPF DVSEC for CXL Device */
+#define CXL_DVSEC_DEVICE_GPF					5
+
+/* 8.1.8: PCIe DVSEC for Flex Bus Port */
+#define CXL_DVSEC_PCIE_FLEXBUS_PORT				7
+
+/* 8.1.9: Register Locator DVSEC */
+#define CXL_DVSEC_REGISTER_LOCATOR				8
+#define   DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET			0xC

Hmm. I'm not particularly keen on not having CXL in the naming for the offset and fields. They get used in places where, to the casual reader, they may look like generic PCI_DVSEC_ defintiions.... the do get very long though, but I'm not sure that's the right place to abbreviate.

Agreed with your comment in the thread that it's really hard to get a good consistent model for what are near enough free form names in a spec (or seem like it when read long after they were defined).

Quoted 20 lines
drivers/cxl/pci.h +8 -2
+#define     DVSEC_REGISTER_LOCATOR_BIR_MASK			GENMASK(2, 0)
+#define	    DVSEC_REGISTER_LOCATOR_BLOCK_IDENTIFIER_MASK	GENMASK(15, 8)
+#define     DVSEC_REGISTER_LOCATOR_BLOCK_OFFSET_LOW_MASK	GENMASK(31, 16) 
 
 /* Register Block Identifier (RBI) */
 enum cxl_regloc_type {
@@ -28,8 +47,11 @@ enum cxl_regloc_type {
 	CXL_REGLOC_RBI_TYPES
 };
 
-#define CXL_REGLOC_RBI_MASK GENMASK(15, 8)
-#define CXL_REGLOC_ADDR_MASK GENMASK(31, 16)
+/* 8.1.10: MLD DVSEC */
+#define CXL_DVSEC_MLD						9
+
+/* 14.16.1 CXL Device Test Capability Advertisement */
+#define CXL_DVSEC_PCIE_TEST_CAPABILITY				10
 
 #define cxl_reg_block(pdev, map)                                               \
 	((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
#75 lore raw

Re: [RFC PATCH v2 13/28] cxl: Flesh out register names

Quoted 3 lines · Jonathan Cameron

On Fri, 22 Oct 2021 11:36:54 -0700
Ben Widawsky <[email protected]> wrote:

[snip]

Quoted 47 lines
Quoted 31 lines
drivers/cxl/pci.h +12 -6
index 12fdcb1b14e5..fe2898b17736 100644
@@ -7,17 +7,36 @@
 
 /*
  * See section 8.1 Configuration Space Registers in the CXL 2.0
- * Specification
+ * Specification. Names are taken straight from the specification with "CXL" and
+ * "DVSEC" redundancies removed.
  */
 #define PCI_DVSEC_HEADER1_LENGTH_MASK	GENMASK(31, 20)
 #define PCI_DVSEC_VENDOR_ID_CXL		0x1E98
-#define PCI_DVSEC_ID_CXL		0x0
 
-#define PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID	0x8
-#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET	0xC
+/* 8.1.3: PCIe DVSEC for CXL Device */
+#define CXL_DVSEC_PCIE_DEVICE					0
 
-/* BAR Indicator Register (BIR) */
-#define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
+/* 8.1.4: Non-CXL Function Map DVSEC */
+#define CXL_DVSEC_FUNCTION_MAP					2
+
+/* 8.1.5: CXL 2.0 Extensions DVSEC for Ports */
+#define CXL_DVSEC_PORT_EXTENSIONS				3
+
+/* 8.1.6: GPF DVSEC for CXL Port */
+#define CXL_DVSEC_PORT_GPF					4

drivers/cxl/pci.h line 36

Quoted 10 lines
drivers/cxl/pci.h +10 -0
+
+/* 8.1.7: GPF DVSEC for CXL Device */
+#define CXL_DVSEC_DEVICE_GPF					5
+
+/* 8.1.8: PCIe DVSEC for Flex Bus Port */
+#define CXL_DVSEC_PCIE_FLEXBUS_PORT				7
+
+/* 8.1.9: Register Locator DVSEC */
+#define CXL_DVSEC_REGISTER_LOCATOR				8
+#define   DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET			0xC

Hmm. I'm not particularly keen on not having CXL in the naming for the offset and fields. They get used in places where, to the casual reader, they may look like generic PCI_DVSEC_ defintiions.... the do get very long though, but I'm not sure that's the right place to abbreviate.

Quoted 4 lines

Agreed with your comment in the thread that it's really hard to get a good consistent model for what are near enough free form names in a spec (or seem like it when read long after they were defined).

Hi Jonathan, thanks for looking.

Well for these in particular, I'd expect the actual register/DVSEC offset to have been read a few lines above, so I'm not too worried about these ones specifically. However, it's a good point as this grows it may become problematic. Do you have a suggestion? My ideal would be for the CXL consortium to publish header files for these things and then we can just grumble about how silly they are and move along, but I'm not sure that will happen.

Anyway... I really don't care what we choose as long as it's consistent. What color would you like?

[snip]

#76 lore raw

Re: [RFC PATCH v2 13/28] cxl: Flesh out register names

Quoted 59 lines · On Wed, 3 Nov 2021 09:03:36 -0700 Ben Widawsky <[email protected]> wrote:
Quoted 3 lines · Jonathan Cameron

On Fri, 22 Oct 2021 11:36:54 -0700
Ben Widawsky <[email protected]> wrote:

[snip]

Quoted 47 lines
Quoted 31 lines
drivers/cxl/pci.h +12 -6
index 12fdcb1b14e5..fe2898b17736 100644
@@ -7,17 +7,36 @@
 
 /*
  * See section 8.1 Configuration Space Registers in the CXL 2.0
- * Specification
+ * Specification. Names are taken straight from the specification with "CXL" and
+ * "DVSEC" redundancies removed.
  */
 #define PCI_DVSEC_HEADER1_LENGTH_MASK	GENMASK(31, 20)
 #define PCI_DVSEC_VENDOR_ID_CXL		0x1E98
-#define PCI_DVSEC_ID_CXL		0x0
 
-#define PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID	0x8
-#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET	0xC
+/* 8.1.3: PCIe DVSEC for CXL Device */
+#define CXL_DVSEC_PCIE_DEVICE					0
 
-/* BAR Indicator Register (BIR) */
-#define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
+/* 8.1.4: Non-CXL Function Map DVSEC */
+#define CXL_DVSEC_FUNCTION_MAP					2
+
+/* 8.1.5: CXL 2.0 Extensions DVSEC for Ports */
+#define CXL_DVSEC_PORT_EXTENSIONS				3
+
+/* 8.1.6: GPF DVSEC for CXL Port */
+#define CXL_DVSEC_PORT_GPF					4

drivers/cxl/pci.h line 36

Quoted 10 lines
drivers/cxl/pci.h +10 -0
+
+/* 8.1.7: GPF DVSEC for CXL Device */
+#define CXL_DVSEC_DEVICE_GPF					5
+
+/* 8.1.8: PCIe DVSEC for Flex Bus Port */
+#define CXL_DVSEC_PCIE_FLEXBUS_PORT				7
+
+/* 8.1.9: Register Locator DVSEC */
+#define CXL_DVSEC_REGISTER_LOCATOR				8
+#define   DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET			0xC  

Hmm. I'm not particularly keen on not having CXL in the naming for the offset and fields. They get used in places where, to the casual reader, they may look like generic PCI_DVSEC_ defintiions.... the do get very long though, but I'm not sure that's the right place to abbreviate.

Quoted 4 lines

Agreed with your comment in the thread that it's really hard to get a good consistent model for what are near enough free form names in a spec (or seem like it when read long after they were defined).

Quoted 8 lines

Hi Jonathan, thanks for looking.

Well for these in particular, I'd expect the actual register/DVSEC offset to have been read a few lines above, so I'm not too worried about these ones specifically. However, it's a good point as this grows it may become problematic. Do you have a suggestion? My ideal would be for the CXL consortium to publish header files for these things and then we can just grumble about how silly they are and move along, but I'm not sure that will happen.

:) You can propose it... - Can you imagine how long those calls would go on for??????

Quoted 3 lines

Anyway... I really don't care what we choose as long as it's consistent. What color would you like?

Blue, no yellow.... Ahhhhhhh!

I think a bit of flexibility on abbreviations is needed when they get insanely long... Meh, we can always rename them all later when we decide the choice was wrong.

Quoted 3 lines

[snip]

#77 lore raw

Re: [RFC PATCH v2 13/28] cxl: Flesh out register names

Quoted 75 lines · Jonathan Cameron
Quoted 59 lines · On Wed, 3 Nov 2021 09:03:36 -0700 Ben Widawsky <[email protected]> wrote:
Quoted 3 lines · Jonathan Cameron

On Fri, 22 Oct 2021 11:36:54 -0700
Ben Widawsky <[email protected]> wrote:

[snip]

Quoted 47 lines
Quoted 31 lines
drivers/cxl/pci.h +12 -6
index 12fdcb1b14e5..fe2898b17736 100644
@@ -7,17 +7,36 @@
 
 /*
  * See section 8.1 Configuration Space Registers in the CXL 2.0
- * Specification
+ * Specification. Names are taken straight from the specification with "CXL" and
+ * "DVSEC" redundancies removed.
  */
 #define PCI_DVSEC_HEADER1_LENGTH_MASK	GENMASK(31, 20)
 #define PCI_DVSEC_VENDOR_ID_CXL		0x1E98
-#define PCI_DVSEC_ID_CXL		0x0
 
-#define PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID	0x8
-#define PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET	0xC
+/* 8.1.3: PCIe DVSEC for CXL Device */
+#define CXL_DVSEC_PCIE_DEVICE					0
 
-/* BAR Indicator Register (BIR) */
-#define CXL_REGLOC_BIR_MASK GENMASK(2, 0)
+/* 8.1.4: Non-CXL Function Map DVSEC */
+#define CXL_DVSEC_FUNCTION_MAP					2
+
+/* 8.1.5: CXL 2.0 Extensions DVSEC for Ports */
+#define CXL_DVSEC_PORT_EXTENSIONS				3
+
+/* 8.1.6: GPF DVSEC for CXL Port */
+#define CXL_DVSEC_PORT_GPF					4

drivers/cxl/pci.h line 36

Quoted 10 lines
drivers/cxl/pci.h +10 -0
+
+/* 8.1.7: GPF DVSEC for CXL Device */
+#define CXL_DVSEC_DEVICE_GPF					5
+
+/* 8.1.8: PCIe DVSEC for Flex Bus Port */
+#define CXL_DVSEC_PCIE_FLEXBUS_PORT				7
+
+/* 8.1.9: Register Locator DVSEC */
+#define CXL_DVSEC_REGISTER_LOCATOR				8
+#define   DVSEC_REGISTER_LOCATOR_BLOCK1_OFFSET			0xC  

Hmm. I'm not particularly keen on not having CXL in the naming for the offset and fields. They get used in places where, to the casual reader, they may look like generic PCI_DVSEC_ defintiions.... the do get very long though, but I'm not sure that's the right place to abbreviate.

Quoted 4 lines

Agreed with your comment in the thread that it's really hard to get a good consistent model for what are near enough free form names in a spec (or seem like it when read long after they were defined).

Quoted 8 lines

Hi Jonathan, thanks for looking.

Well for these in particular, I'd expect the actual register/DVSEC offset to have been read a few lines above, so I'm not too worried about these ones specifically. However, it's a good point as this grows it may become problematic. Do you have a suggestion? My ideal would be for the CXL consortium to publish header files for these things and then we can just grumble about how silly they are and move along, but I'm not sure that will happen.

:) You can propose it... - Can you imagine how long those calls would go on for??????

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

Anyway... I really don't care what we choose as long as it's consistent. What color would you like?

Blue, no yellow.... Ahhhhhhh!

I think a bit of flexibility on abbreviations is needed when they get insanely long... Meh, we can always rename them all later when we decide the choice was wrong.

Two very good choices. Ack. I'll respin. Let me spell out though in case it wasn't clear, my goal was to make something grepable, which abbreviations take away from. This is because as the 3.0 spec goes through I'm noticing our chapter references are no longer 100% accurate (annoying!).

Quoted 5 lines
Quoted 4 lines

[snip]

#78 lore raw

RFC PATCH v2 14/28 cxl: Hide devm host for ports

In reply to Ben Widawsky

Generally, a platform will only have a single instance of an entity which has a platform specific CXL driver. For the time being, this is only the ACPI0017 device defined as part of the CXL specification and ancillary specs (ACPI). With this in mind it becomes easy to drop the need to pass this around for devm functions which want to know the root host. That change allows future work with the mem/port driver to avoid having to determine the root host for port creation for endpoints and switches.

There's some technical dept leftover with regard to cxl_test.

---
 drivers/cxl/acpi.c     | 15 ++++++++++++---
 drivers/cxl/core/bus.c | 33 +++++++++++++++++++++++++++++----
 drivers/cxl/cxl.h      |  5 ++++-
 3 files changed, 45 insertions(+), 8 deletions(-)
drivers/cxl/acpi.c +12 -3
index 1cc3a74c16bd..f6b78736c933 100644
@@ -298,8 +298,7 @@ static int add_host_bridge_uport(struct device *match, void *arg)
 		return -ENODEV;
 	}
 
-	port = devm_cxl_add_port(host, match, dport->component_reg_phys,
-				 root_port);
+	port = devm_cxl_add_port(match, dport->component_reg_phys, root_port);
 	if (IS_ERR(port))
 		return PTR_ERR(port);
 	dev_dbg(host, "%s: add: %s\n", dev_name(match), dev_name(&port->dev));
@@ -467,7 +466,9 @@ static int cxl_acpi_probe(struct platform_device *pdev)
 	struct device *host = &pdev->dev;
 	struct acpi_device *adev = ACPI_COMPANION(host);
 
-	root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
+	cxl_register_root(host);
+
+	root_port = devm_cxl_add_port(host, CXL_RESOURCE_NONE, NULL);
 	if (IS_ERR(root_port))
 		return PTR_ERR(root_port);
 	dev_dbg(host, "add: %s\n", dev_name(&root_port->dev));
@@ -513,6 +514,13 @@ static int cxl_acpi_probe(struct platform_device *pdev)
 	return 0;
 }
 
+static int cxl_acpi_remove(struct platform_device *pdev)
+{
+	cxl_unregister_root();
+
+	return 0;
+}
+
 static bool native_acpi0017 = true;
 
 static const struct acpi_device_id cxl_acpi_ids[] = {
@@ -523,6 +531,7 @@ MODULE_DEVICE_TABLE(acpi, cxl_acpi_ids);
 
 static struct platform_driver cxl_acpi_driver = {
 	.probe = cxl_acpi_probe,
+	.remove = cxl_acpi_remove,
 	.driver = {
 		.name = KBUILD_MODNAME,
 		.acpi_match_table = cxl_acpi_ids,
drivers/cxl/core/bus.c +29 -4
index 03394a3ae75f..c7e1894d503b 100644
@@ -27,6 +27,32 @@
 static DEFINE_IDA(cxl_port_ida);
 static LIST_HEAD(cxl_root_ports);
 static DECLARE_RWSEM(root_port_sem);
+static DECLARE_RWSEM(root_host_sem);
+
+static struct device *cxl_root_host;
+
+int cxl_register_root(struct device *host)
+{
+	down_write(&root_host_sem);
+	if (cxl_root_host)
+		return -EBUSY;
+
+	get_device(host);
+	cxl_root_host = host;
+	up_write(&root_host_sem);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(cxl_register_root);
+
+void cxl_unregister_root(void)
+{
+	down_write(&root_host_sem);
+	put_device(cxl_root_host);
+	cxl_root_host = NULL;
+	up_write(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(cxl_unregister_root);
 
 static ssize_t devtype_show(struct device *dev, struct device_attribute *attr,
 			    char *buf)
@@ -375,12 +401,11 @@ static struct cxl_port *cxl_port_alloc(struct device *uport,
 
 /**
  * devm_cxl_add_port - register a cxl_port in CXL memory decode hierarchy
- * @host: host device for devm operations
  * @uport: "physical" device implementing this upstream port
  * @component_reg_phys: (optional) for configurable cxl_port instances
  * @parent_port: next hop up in the CXL memory decode hierarchy
  */
-struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
+struct cxl_port *devm_cxl_add_port(struct device *uport,
 				   resource_size_t component_reg_phys,
 				   struct cxl_port *parent_port)
 {
@@ -404,11 +429,11 @@ struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
 	if (rc)
 		goto err;
 
-	rc = devm_add_action_or_reset(host, unregister_port, port);
+	rc = devm_add_action_or_reset(cxl_root_host, unregister_port, port);
 	if (rc)
 		return ERR_PTR(rc);
 
-	rc = devm_cxl_link_uport(host, port);
+	rc = devm_cxl_link_uport(cxl_root_host, port);
 	if (rc)
 		return ERR_PTR(rc);
 
drivers/cxl/cxl.h +4 -1
index d26d97e1098d..419c2e2db6f0 100644
@@ -184,6 +184,9 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,
 #define CXL_RESOURCE_NONE ((resource_size_t) -1)
 #define CXL_TARGET_STRLEN 20
 
+int cxl_register_root(struct device *host);
+void cxl_unregister_root(void);
+
 /*
  * cxl_decoder flags that define the type of memory / devices this
  * decoder supports as well as configuration lock status See "CXL 2.0
@@ -303,7 +306,7 @@ struct cxl_dport {
 };
 
 struct cxl_port *to_cxl_port(struct device *dev);
-struct cxl_port *devm_cxl_add_port(struct device *host, struct device *uport,
+struct cxl_port *devm_cxl_add_port(struct device *uport,
 				   resource_size_t component_reg_phys,
 				   struct cxl_port *parent_port);
 
Signature · 1 line
2.33.1
#79 lore raw

Re: [RFC PATCH v2 14/28] cxl: Hide devm host for ports

Quoted 9 lines · Ben Widawsky

Generally, a platform will only have a single instance of an entity which has a platform specific CXL driver. For the time being, this is only the ACPI0017 device defined as part of the CXL specification and ancillary specs (ACPI). With this in mind it becomes easy to drop the need to pass this around for devm functions which want to know the root host. That change allows future work with the mem/port driver to avoid having to determine the root host for port creation for endpoints and switches.

Given the revelation earlier that we don't need to devm registration underneath the root object to coordinate descendant device teardown, this change can be dropped.

#80 lore raw

RFC PATCH v2 15/28 cxl/core: Introduce API to scan switch ports

In reply to Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +145 -0

157 lines not on this page · raw message

drivers/cxl/core/pci.c new file +99 -0

99 lines not on this page · raw message

drivers/cxl/cxl.h +2 -0
index 419c2e2db6f0..03b414462416 100644
@@ -309,6 +309,8 @@ struct cxl_port *to_cxl_port(struct device *dev);
 struct cxl_port *devm_cxl_add_port(struct device *uport,
 				   resource_size_t component_reg_phys,
 				   struct cxl_port *parent_port);
+void devm_cxl_remove_port(struct cxl_port *port);
+void cxl_scan_ports(struct cxl_dport *root_port);
 
 int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
 		  resource_size_t component_reg_phys, bool root_port);
drivers/cxl/pci.h +6 -0
index fe2898b17736..9d6ca77d3e14 100644
@@ -15,6 +15,8 @@
 
 /* 8.1.3: PCIe DVSEC for CXL Device */
 #define CXL_DVSEC_PCIE_DEVICE					0
+#define   DVSEC_PCIE_DEVICE_CONTROL_OFFSET			0xC
+#define     DVSEC_PCIE_DEVICE_MEM_ENABLE			BIT(2)
 
 /* 8.1.4: Non-CXL Function Map DVSEC */
 #define CXL_DVSEC_FUNCTION_MAP					2
@@ -57,4 +59,8 @@ enum cxl_regloc_type {
 	((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
 			   (map)->block_offset))
 
+bool is_cxl_switch_usp(struct device *dev);
+bool is_cxl_switch_dsp(struct device *dev);
+bool is_cxl_mem_enabled(struct pci_dev *pdev);
+
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +1 -1
index ebbfb72ae995..3ddfd7673a56 100644
@@ -170,7 +170,7 @@ static int enumerate_hdm_decoders(struct cxl_port *port,
 		if (rc)
 			put_device(&cxld->dev);
 		else
-			rc = cxl_decoder_autoremove(port->uport->parent, cxld);
+			rc = cxl_decoder_autoremove(&port->dev, cxld);
 		if (rc)
 			dev_err(&port->dev, "Failed to add decoder\n");
 	}
tools/testing/cxl/Kbuild +1 -0
index 86deba8308a1..46db4dd345a0 100644
@@ -31,6 +31,7 @@ cxl_core-y += $(CXL_CORE_SRC)/pmem.o
 cxl_core-y += $(CXL_CORE_SRC)/regs.o
 cxl_core-y += $(CXL_CORE_SRC)/memdev.o
 cxl_core-y += $(CXL_CORE_SRC)/mbox.o
+cxl_core-y += $(CXL_CORE_SRC)/pci.o
 cxl_core-y += config_check.o
 
 cxl_core-y += mock_pmem.o
Signature · 1 line
2.33.1
#81 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

Quoted 36 lines
drivers/cxl/core/bus.c +36 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 508

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       port = devm_cxl_add_port(dev, cxl_reg_block(pdev, &map), parent_port);

This is broken because the pci device being used here does not have a driver that knows about CXL bus events.

Quoted 20 lines
drivers/cxl/core/bus.c +20 -0
+       put_device(&parent_port->dev);
+       if (IS_ERR(port))
+               dev_err(dev, "Failed to add upstream port %ld\n",
+                       PTR_ERR(port));
+       else
+               dev_dbg(dev, "Added CXL port\n");
+
+       return rc;
+}
+
+static int add_downstream_port(struct pci_dev *pdev)
+{
+       resource_size_t creg = CXL_RESOURCE_NONE;
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       u32 lnkcap, port_num;
+       int rc;
+
+       /*

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 538

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+        * Ports are to be scanned from top down. Therefore, the upstream port
+        * must already exist.
+        */
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent));
+       if (!parent_port)
+               return -ENODEV;
+
+       /*
+        * The spec mandates component registers are present but the
+        * driver does not.

What is this trying to convey?

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 550

Quoted 12 lines
drivers/cxl/core/bus.c +12 -0
+        */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+       if (!rc)
+               creg = cxl_reg_block(pdev, &map);
+
+       if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
+                                 &lnkcap) != PCIBIOS_SUCCESSFUL)
+               return 1;
+       port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
+
+       rc = cxl_add_dport(parent_port, dev, port_num, creg, false);
+       put_device(&parent_port->dev);

What get_device() is this paired with?

Quoted 71 lines
drivers/cxl/core/bus.c +42 -0
+       if (rc)
+               dev_err(dev, "Failed to add downstream port to %s\n",
+                       dev_name(&parent_port->dev));
+       else
+               dev_dbg(dev, "Added downstream port to %s\n",
+                       dev_name(&parent_port->dev));
+
+       return rc;
+}
+
+static int match_add_ports(struct pci_dev *pdev, void *data)
+{
+       struct device *dev = &pdev->dev;
+       struct device *host = data;
+       int rc;
+
+       /* This port has already been added... */
+       if (find_cxl_port(pdev))
+               return 0;
+
+       if (is_cxl_switch_usp((dev)))
+               rc = add_upstream_port(host, pdev);
+
+       if (is_cxl_switch_dsp((dev)))
+               rc = add_downstream_port(pdev);
+
+       return rc;
+}
+
+/**
+ * cxl_scan_ports() - Adds all ports for the subtree beginning with @dport
+ * @dport: Beginning node of the CXL topology
+ */
+void cxl_scan_ports(struct cxl_dport *dport)
+{
+       struct device *d = dport->dport;
+       struct pci_dev *pdev = to_pci_dev(d);
+
+       pci_walk_bus(pdev->bus, match_add_ports, &dport->port->dev);
+}
+EXPORT_SYMBOL_GPL(cxl_scan_ports);
+
 static struct cxl_dport *find_dport(struct cxl_port *port, int id)
 {
        struct cxl_dport *dport;
drivers/cxl/core/pci.c new file +20 -0
new file mode 100644
index 000000000000..c0cbe984c778
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2021 Intel Corporation. All rights reserved. */
+#include <linux/device.h>
+#include <linux/pci.h>
+#include <pci.h>
+
+/**
+ * DOC: cxl pci
+ *
+ * Compute Express Link protocols are layered on top of PCIe. CXL core provides
+ * a set of helpers for CXL interactions which occur via PCIe.
+ */
+
+/**
+ * is_cxl_mem_enabled() - Does the device understand CXL.mem protocol
+ * @pdev: The PCI device for which to determine CXL enablement
+ *
+ * This is the most discrete determination as to whether a device supports
+ * CXL.mem protocol. At a minimum, a CXL device must advertise it is capable of
+ * negotiating the CXL.mem protocol while operating in Flex Bus.CXL mode. There

drivers/cxl/core/pci.c line 30

Quoted 10 lines
drivers/cxl/core/pci.c new file +10 -0
+ * are other determining factors as to whether CXL.mem protocol is supported in
+ * the path from root port to endpoint. Those other factors require a more
+ * comprehensive survey of the CXL topology and would use is_cxl_mem_enabled()
+ * as a cursory check.
+ *
+ * If the PCI device is enabled for CXL.mem protocol return true; otherwise
+ * return false.
+ *
+ * TODO: Is there other architecturally visible state that can be used to infer
+ *       CXL.mem protocol support?

I was expecting that cxl_port->dev.driver != NULL is the CXL subsystem source of truth.

Quoted 11 lines
drivers/cxl/core/pci.c new file +11 -0
+ */
+bool is_cxl_mem_enabled(struct pci_dev *pdev)
+{
+       int pcie_dvsec;
+       u16 dvsec_ctrl;
+
+       pcie_dvsec = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
+                                              CXL_DVSEC_PCIE_DEVICE);
+       if (!pcie_dvsec) {
+               dev_info(&pdev->dev,
+                        "Unable to determine CXL protocol support");

drivers/cxl/core/pci.c line 51

Quoted 10 lines
drivers/cxl/core/pci.c new file +10 -0
+               return false;
+       }
+
+       pci_read_config_word(pdev,
+                            pcie_dvsec + DVSEC_PCIE_DEVICE_CONTROL_OFFSET,
+                            &dvsec_ctrl);
+       if (!(dvsec_ctrl & DVSEC_PCIE_DEVICE_MEM_ENABLE)) {
+               dev_info(&pdev->dev, "CXL.mem protocol not enabled on device");
+               return false;
+       }

Per above, doesn't the port driver already check this?

drivers/cxl/core/pci.c line 55

Quoted 4 lines
drivers/cxl/core/pci.c new file +4 -0
+
+       return true;
+}
+EXPORT_SYMBOL_GPL(is_cxl_mem_enabled);

Also per above, can't this stay internal to the port driver?

Quoted 12 lines
drivers/cxl/core/pci.c new file +12 -0
+
+/**
+ * is_cxl_switch_usp() - Is the device a CXL.mem enabled switch
+ * @dev: Device to query for switch type
+ *
+ * If the device is a CXL.mem capable upstream switch port return true;
+ * otherwise return false.
+ */
+bool is_cxl_switch_usp(struct device *dev)
+{
+       struct pci_dev *pdev;
+

drivers/cxl/core/pci.c line 77

Quoted 10 lines
drivers/cxl/core/pci.c new file +10 -0
+       if (!dev_is_pci(dev))
+               return false;
+
+       pdev = to_pci_dev(dev);
+
+       return pci_is_pcie(pdev) &&
+              pci_pcie_type(pdev) == PCI_EXP_TYPE_UPSTREAM &&
+              is_cxl_mem_enabled(pdev);
+}
+EXPORT_SYMBOL_GPL(is_cxl_switch_usp);

Who uses this api outside of the core?

Quoted 12 lines
drivers/cxl/core/pci.c new file +12 -0
+
+/**
+ * is_cxl_switch_dsp() - Is the device a CXL.mem enabled switch
+ * @dev: Device to query for switch type
+ *
+ * If the device is a CXL.mem capable downstream switch port return true;
+ * otherwise return false.
+ */
+bool is_cxl_switch_dsp(struct device *dev)
+{
+       struct pci_dev *pdev;
+

drivers/cxl/core/pci.c line 99

Quoted 10 lines
drivers/cxl/core/pci.c new file +10 -0
+       if (!dev_is_pci(dev))
+               return false;
+
+       pdev = to_pci_dev(dev);
+
+       return pci_is_pcie(pdev) &&
+              pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM &&
+              is_cxl_mem_enabled(pdev);
+}
+EXPORT_SYMBOL_GPL(is_cxl_switch_dsp);

Same question... at a minimum wait to add exports until there is a user.

Quoted 40 lines
drivers/cxl/cxl.h +2 -0
index 419c2e2db6f0..03b414462416 100644
@@ -309,6 +309,8 @@ struct cxl_port *to_cxl_port(struct device *dev);
 struct cxl_port *devm_cxl_add_port(struct device *uport,
                                   resource_size_t component_reg_phys,
                                   struct cxl_port *parent_port);
+void devm_cxl_remove_port(struct cxl_port *port);
+void cxl_scan_ports(struct cxl_dport *root_port);
 
 int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
                  resource_size_t component_reg_phys, bool root_port);
drivers/cxl/pci.h +6 -0
index fe2898b17736..9d6ca77d3e14 100644
@@ -15,6 +15,8 @@
 
 /* 8.1.3: PCIe DVSEC for CXL Device */
 #define CXL_DVSEC_PCIE_DEVICE                                  0
+#define   DVSEC_PCIE_DEVICE_CONTROL_OFFSET                     0xC
+#define     DVSEC_PCIE_DEVICE_MEM_ENABLE                       BIT(2)
 
 /* 8.1.4: Non-CXL Function Map DVSEC */
 #define CXL_DVSEC_FUNCTION_MAP                                 2
@@ -57,4 +59,8 @@ enum cxl_regloc_type {
        ((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
                           (map)->block_offset))
 
+bool is_cxl_switch_usp(struct device *dev);
+bool is_cxl_switch_dsp(struct device *dev);
+bool is_cxl_mem_enabled(struct pci_dev *pdev);
+
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +0 -0
index ebbfb72ae995..3ddfd7673a56 100644

drivers/cxl/port.c static int enumerate_hdm_decoders(struct cxl_port *port, · line 173

Quoted 5 lines
drivers/cxl/port.c +1 -1
@@ -170,7 +170,7 @@ static int enumerate_hdm_decoders(struct cxl_port *port,
                if (rc)
                        put_device(&cxld->dev);
                else
-                       rc = cxl_decoder_autoremove(port->uport->parent, cxld);
+                       rc = cxl_decoder_autoremove(&port->dev, cxld);

Looks like a hunk that got added to the wrong patch.

Quoted 18 lines
drivers/cxl/port.c +0 -0
                if (rc)
                        dev_err(&port->dev, "Failed to add decoder\n");
        }
tools/testing/cxl/Kbuild +1 -1
index 86deba8308a1..46db4dd345a0 100644
@@ -31,6 +31,7 @@ cxl_core-y += $(CXL_CORE_SRC)/pmem.o
 cxl_core-y += $(CXL_CORE_SRC)/regs.o
 cxl_core-y += $(CXL_CORE_SRC)/memdev.o
 cxl_core-y += $(CXL_CORE_SRC)/mbox.o
+cxl_core-y += $(CXL_CORE_SRC)/pci.o
 cxl_core-y += config_check.o
 
 cxl_core-y += mock_pmem.o
--

2.33.1

#82 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 72 lines · Dan Williams
Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 
Quoted 16 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

We had discussed this previously. I was running into an issue when unloading cxl_mem. I needed a way to remove the endpoint port and this was your recommendation. Are you suggesting if the chain is set up correctly, I don't need to do anything?

I don't remember exactly what was blowing up but I can try again after things are properly parented.

Quoted 42 lines
Quoted 42 lines
drivers/cxl/core/bus.c +42 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

The goal is to be able to find the next PCIe port up in the chain.

My understanding was:
pdev = PCIe upstream switch
pdev->dev.parent = PCIe downstream switch connected to pdev. pdev->dev.parent->parent = PCIe upstream switch connected to pdev->dev.parent

I was unable to find an idiomatic way to do that. I'm open to suggestions.

Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 508

Quoted 5 lines
drivers/cxl/core/bus.c +4 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       port = devm_cxl_add_port(dev, cxl_reg_block(pdev, &map), parent_port);

This is broken because the pci device being used here does not have a driver that knows about CXL bus events.

I don't understand this, but I'd like to. Doesn't this make a port device which gets probed by the port driver? Why does the PCI device matter?

(I'll mention again, switch code is not tested).

Quoted 25 lines
Quoted 25 lines
drivers/cxl/core/bus.c +24 -0
+       put_device(&parent_port->dev);
+       if (IS_ERR(port))
+               dev_err(dev, "Failed to add upstream port %ld\n",
+                       PTR_ERR(port));
+       else
+               dev_dbg(dev, "Added CXL port\n");
+
+       return rc;
+}
+
+static int add_downstream_port(struct pci_dev *pdev)
+{
+       resource_size_t creg = CXL_RESOURCE_NONE;
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       u32 lnkcap, port_num;
+       int rc;
+
+       /*
+        * Ports are to be scanned from top down. Therefore, the upstream port
+        * must already exist.
+        */
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent));
Quoted 9 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 538

Quoted 6 lines
drivers/cxl/core/bus.c +6 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       /*
+        * The spec mandates component registers are present but the
+        * driver does not.

What is this trying to convey?

That I'm not validating the hardware, and even though component registers are mandatory, the driver will move on even if they're not found. This functionality may need to change in the future and so I left the comment there.

Quoted 6 lines
Quoted 6 lines
drivers/cxl/core/bus.c +6 -0
+        */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
+       if (!rc)
+               creg = cxl_reg_block(pdev, &map);
+
+       if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
Quoted 9 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 550

Quoted 6 lines
drivers/cxl/core/bus.c +6 -0
+                                 &lnkcap) != PCIBIOS_SUCCESSFUL)
+               return 1;
+       port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
+
+       rc = cxl_add_dport(parent_port, dev, port_num, creg, false);
+       put_device(&parent_port->dev);

What get_device() is this paired with?

find_cxl_port() holds a reference. I had a comment somewhere to that effect but it seems to have disappeared. Since it sounds like this code might be changing, I won't fix that yet.

Quoted 109 lines
Quoted 71 lines
drivers/cxl/core/bus.c +42 -0
+       if (rc)
+               dev_err(dev, "Failed to add downstream port to %s\n",
+                       dev_name(&parent_port->dev));
+       else
+               dev_dbg(dev, "Added downstream port to %s\n",
+                       dev_name(&parent_port->dev));
+
+       return rc;
+}
+
+static int match_add_ports(struct pci_dev *pdev, void *data)
+{
+       struct device *dev = &pdev->dev;
+       struct device *host = data;
+       int rc;
+
+       /* This port has already been added... */
+       if (find_cxl_port(pdev))
+               return 0;
+
+       if (is_cxl_switch_usp((dev)))
+               rc = add_upstream_port(host, pdev);
+
+       if (is_cxl_switch_dsp((dev)))
+               rc = add_downstream_port(pdev);
+
+       return rc;
+}
+
+/**
+ * cxl_scan_ports() - Adds all ports for the subtree beginning with @dport
+ * @dport: Beginning node of the CXL topology
+ */
+void cxl_scan_ports(struct cxl_dport *dport)
+{
+       struct device *d = dport->dport;
+       struct pci_dev *pdev = to_pci_dev(d);
+
+       pci_walk_bus(pdev->bus, match_add_ports, &dport->port->dev);
+}
+EXPORT_SYMBOL_GPL(cxl_scan_ports);
+
 static struct cxl_dport *find_dport(struct cxl_port *port, int id)
 {
        struct cxl_dport *dport;
drivers/cxl/core/pci.c new file +20 -0
new file mode 100644
index 000000000000..c0cbe984c778
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright(c) 2021 Intel Corporation. All rights reserved. */
+#include <linux/device.h>
+#include <linux/pci.h>
+#include <pci.h>
+
+/**
+ * DOC: cxl pci
+ *
+ * Compute Express Link protocols are layered on top of PCIe. CXL core provides
+ * a set of helpers for CXL interactions which occur via PCIe.
+ */
+
+/**
+ * is_cxl_mem_enabled() - Does the device understand CXL.mem protocol
+ * @pdev: The PCI device for which to determine CXL enablement
+ *
+ * This is the most discrete determination as to whether a device supports
+ * CXL.mem protocol. At a minimum, a CXL device must advertise it is capable of
+ * negotiating the CXL.mem protocol while operating in Flex Bus.CXL mode. There

drivers/cxl/core/pci.c line 30

Quoted 10 lines
drivers/cxl/core/pci.c new file +10 -0
+ * are other determining factors as to whether CXL.mem protocol is supported in
+ * the path from root port to endpoint. Those other factors require a more
+ * comprehensive survey of the CXL topology and would use is_cxl_mem_enabled()
+ * as a cursory check.
+ *
+ * If the PCI device is enabled for CXL.mem protocol return true; otherwise
+ * return false.
+ *
+ * TODO: Is there other architecturally visible state that can be used to infer
+ *       CXL.mem protocol support?

I was expecting that cxl_port->dev.driver != NULL is the CXL subsystem source of truth.

Quoted 11 lines
drivers/cxl/core/pci.c new file +11 -0
+ */
+bool is_cxl_mem_enabled(struct pci_dev *pdev)
+{
+       int pcie_dvsec;
+       u16 dvsec_ctrl;
+
+       pcie_dvsec = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
+                                              CXL_DVSEC_PCIE_DEVICE);
+       if (!pcie_dvsec) {
+               dev_info(&pdev->dev,
+                        "Unable to determine CXL protocol support");

drivers/cxl/core/pci.c line 51

Quoted 10 lines
drivers/cxl/core/pci.c new file +10 -0
+               return false;
+       }
+
+       pci_read_config_word(pdev,
+                            pcie_dvsec + DVSEC_PCIE_DEVICE_CONTROL_OFFSET,
+                            &dvsec_ctrl);
+       if (!(dvsec_ctrl & DVSEC_PCIE_DEVICE_MEM_ENABLE)) {
+               dev_info(&pdev->dev, "CXL.mem protocol not enabled on device");
+               return false;
+       }

Per above, doesn't the port driver already check this?

Quoted 7 lines

drivers/cxl/core/pci.c line 55

Quoted 4 lines
drivers/cxl/core/pci.c new file +4 -0
+
+       return true;
+}
+EXPORT_SYMBOL_GPL(is_cxl_mem_enabled);

Also per above, can't this stay internal to the port driver?

At one point I think there was another consumer, so it got put here. I can be internal to the port driver.

Quoted 41 lines
Quoted 12 lines
drivers/cxl/core/pci.c new file +12 -0
+
+/**
+ * is_cxl_switch_usp() - Is the device a CXL.mem enabled switch
+ * @dev: Device to query for switch type
+ *
+ * If the device is a CXL.mem capable upstream switch port return true;
+ * otherwise return false.
+ */
+bool is_cxl_switch_usp(struct device *dev)
+{
+       struct pci_dev *pdev;
+

drivers/cxl/core/pci.c line 77

Quoted 10 lines
drivers/cxl/core/pci.c new file +10 -0
+       if (!dev_is_pci(dev))
+               return false;
+
+       pdev = to_pci_dev(dev);
+
+       return pci_is_pcie(pdev) &&
+              pci_pcie_type(pdev) == PCI_EXP_TYPE_UPSTREAM &&
+              is_cxl_mem_enabled(pdev);
+}
+EXPORT_SYMBOL_GPL(is_cxl_switch_usp);

Who uses this api outside of the core?

Quoted 16 lines
drivers/cxl/core/pci.c new file +16 -0
+
+/**
+ * is_cxl_switch_dsp() - Is the device a CXL.mem enabled switch
+ * @dev: Device to query for switch type
+ *
+ * If the device is a CXL.mem capable downstream switch port return true;
+ * otherwise return false.
+ */
+bool is_cxl_switch_dsp(struct device *dev)
+{
+       struct pci_dev *pdev;
+
+       if (!dev_is_pci(dev))
+               return false;
+
+       pdev = to_pci_dev(dev);
Quoted 9 lines

drivers/cxl/core/pci.c line 99

Quoted 6 lines
drivers/cxl/core/pci.c new file +6 -0
+
+       return pci_is_pcie(pdev) &&
+              pci_pcie_type(pdev) == PCI_EXP_TYPE_DOWNSTREAM &&
+              is_cxl_mem_enabled(pdev);
+}
+EXPORT_SYMBOL_GPL(is_cxl_switch_dsp);

Same question... at a minimum wait to add exports until there is a user.

Okay. It gets used by the mem driver, but I can remove the export here.

Quoted 39 lines
Quoted 39 lines
drivers/cxl/cxl.h +2 -0
index 419c2e2db6f0..03b414462416 100644
@@ -309,6 +309,8 @@ struct cxl_port *to_cxl_port(struct device *dev);
 struct cxl_port *devm_cxl_add_port(struct device *uport,
                                   resource_size_t component_reg_phys,
                                   struct cxl_port *parent_port);
+void devm_cxl_remove_port(struct cxl_port *port);
+void cxl_scan_ports(struct cxl_dport *root_port);
 
 int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
                  resource_size_t component_reg_phys, bool root_port);
drivers/cxl/pci.h +6 -0
index fe2898b17736..9d6ca77d3e14 100644
@@ -15,6 +15,8 @@
 
 /* 8.1.3: PCIe DVSEC for CXL Device */
 #define CXL_DVSEC_PCIE_DEVICE                                  0
+#define   DVSEC_PCIE_DEVICE_CONTROL_OFFSET                     0xC
+#define     DVSEC_PCIE_DEVICE_MEM_ENABLE                       BIT(2)
 
 /* 8.1.4: Non-CXL Function Map DVSEC */
 #define CXL_DVSEC_FUNCTION_MAP                                 2
@@ -57,4 +59,8 @@ enum cxl_regloc_type {
        ((resource_size_t)(pci_resource_start(pdev, (map)->barno) +            \
                           (map)->block_offset))
 
+bool is_cxl_switch_usp(struct device *dev);
+bool is_cxl_switch_dsp(struct device *dev);
+bool is_cxl_mem_enabled(struct pci_dev *pdev);
+
 #endif /* __CXL_PCI_H__ */
drivers/cxl/port.c +0 -0
index ebbfb72ae995..3ddfd7673a56 100644
Quoted 8 lines

drivers/cxl/port.c static int enumerate_hdm_decoders(struct cxl_port *port, · line 173

Quoted 6 lines
drivers/cxl/port.c +1 -1
@@ -170,7 +170,7 @@ static int enumerate_hdm_decoders(struct cxl_port *port,
                if (rc)
                        put_device(&cxld->dev);
                else
-                       rc = cxl_decoder_autoremove(port->uport->parent, cxld);
+                       rc = cxl_decoder_autoremove(&port->dev, cxld);

Looks like a hunk that got added to the wrong patch.

Yep

Quoted 19 lines
Quoted 19 lines
drivers/cxl/port.c +0 -0
                if (rc)
                        dev_err(&port->dev, "Failed to add decoder\n");
        }
tools/testing/cxl/Kbuild +1 -1
index 86deba8308a1..46db4dd345a0 100644
@@ -31,6 +31,7 @@ cxl_core-y += $(CXL_CORE_SRC)/pmem.o
 cxl_core-y += $(CXL_CORE_SRC)/regs.o
 cxl_core-y += $(CXL_CORE_SRC)/memdev.o
 cxl_core-y += $(CXL_CORE_SRC)/mbox.o
+cxl_core-y += $(CXL_CORE_SRC)/pci.o
 cxl_core-y += config_check.o
 
 cxl_core-y += mock_pmem.o
--

2.33.1

#83 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 91 lines · Ben Widawsky
Quoted 72 lines · Dan Williams
Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 
Quoted 16 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

Quoted 4 lines

We had discussed this previously. I was running into an issue when unloading cxl_mem. I needed a way to remove the endpoint port and this was your recommendation. Are you suggesting if the chain is set up correctly, I don't need to do anything?

I think if the chain is set up correctly then you don't need to do anything special. The endpoint port would be devm registered by the cxl_memdev driver to its parent cxl_port provided that port is actively attached to its driver.

Quoted 2 lines

I don't remember exactly what was blowing up but I can try again after things are properly parented.

Cool.

Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
drivers/cxl/core/bus.c +42 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

Quoted 8 lines

The goal is to be able to find the next PCIe port up in the chain.

My understanding was:
pdev = PCIe upstream switch
pdev->dev.parent = PCIe downstream switch connected to pdev. pdev->dev.parent->parent = PCIe upstream switch connected to pdev->dev.parent

I was unable to find an idiomatic way to do that. I'm open to suggestions.

Oh ok, I see it now, but I think this can be done in pure CXL terms and generic devices with the assumption that the parent device of a cxl_memdev must be a dport. Then this works whether the parent port is a platform device like ACPI or cxl_test, or a PCIe device.

static int port_has_dport(struct device *dev, const void *dport_dev)

{
        int found = 0;
        struct cxl_port *port;
        struct cxl_dport *dport;

        if (dev->type != &cxl_port_type)
                return 0;
        port = to_cxl_port(dev);

        device_lock(&port->dev);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dport_dev) {
                        found = 1;
                        break;
                }
        device_unlock(&port->dev);

        return found;
}

struct cxl_port *find_parent_cxl_port(struct cxl_memdev *cxlmd)

{
        return bus_find_device(&cxl_bus_type, NULL, cxlmd->dev.parent,
                               port_has_dport);
}
Quoted 11 lines
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 508

Quoted 5 lines
drivers/cxl/core/bus.c +4 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       port = devm_cxl_add_port(dev, cxl_reg_block(pdev, &map), parent_port);

This is broken because the pci device being used here does not have a driver that knows about CXL bus events.

I don't understand this, but I'd like to. Doesn't this make a port device which gets probed by the port driver? Why does the PCI device matter?

I am reacting to the first argument of this call being @dev that came from the pci_dev that was passed in to be the "host" for the devm operation. The devm release action triggers at driver unbind of that host device, but that doesn't make sense because the driver for a switch has nothing to do with CXL operation.

Quoted 38 lines

(I'll mention again, switch code is not tested).

Quoted 25 lines
Quoted 25 lines
drivers/cxl/core/bus.c +24 -0
+       put_device(&parent_port->dev);
+       if (IS_ERR(port))
+               dev_err(dev, "Failed to add upstream port %ld\n",
+                       PTR_ERR(port));
+       else
+               dev_dbg(dev, "Added CXL port\n");
+
+       return rc;
+}
+
+static int add_downstream_port(struct pci_dev *pdev)
+{
+       resource_size_t creg = CXL_RESOURCE_NONE;
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       u32 lnkcap, port_num;
+       int rc;
+
+       /*
+        * Ports are to be scanned from top down. Therefore, the upstream port
+        * must already exist.
+        */
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent));
Quoted 9 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 538

Quoted 6 lines
drivers/cxl/core/bus.c +6 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       /*
+        * The spec mandates component registers are present but the
+        * driver does not.

What is this trying to convey?

Quoted 3 lines

That I'm not validating the hardware, and even though component registers are mandatory, the driver will move on even if they're not found. This functionality may need to change in the future and so I left the comment there.

I think that could be conveyed without comment with something like:

        if (rc)
                creg = CXL_RESOURCE_NONE;
        else
                creg = cxl_reg_block(pdev, &map);
#84 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 194 lines · Dan Williams
Quoted 91 lines · Ben Widawsky
Quoted 72 lines · Dan Williams
Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 
Quoted 16 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

Quoted 4 lines

We had discussed this previously. I was running into an issue when unloading cxl_mem. I needed a way to remove the endpoint port and this was your recommendation. Are you suggesting if the chain is set up correctly, I don't need to do anything?

I think if the chain is set up correctly then you don't need to do anything special. The endpoint port would be devm registered by the cxl_memdev driver to its parent cxl_port provided that port is actively attached to its driver.

Quoted 2 lines

I don't remember exactly what was blowing up but I can try again after things are properly parented.

Cool.

Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
drivers/cxl/core/bus.c +42 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

Quoted 8 lines

The goal is to be able to find the next PCIe port up in the chain.

My understanding was:
pdev = PCIe upstream switch
pdev->dev.parent = PCIe downstream switch connected to pdev. pdev->dev.parent->parent = PCIe upstream switch connected to pdev->dev.parent

I was unable to find an idiomatic way to do that. I'm open to suggestions.

Oh ok, I see it now, but I think this can be done in pure CXL terms and generic devices with the assumption that the parent device of a cxl_memdev must be a dport. Then this works whether the parent port is a platform device like ACPI or cxl_test, or a PCIe device.

static int port_has_dport(struct device *dev, const void *dport_dev)

{
        int found = 0;
        struct cxl_port *port;
        struct cxl_dport *dport;

        if (dev->type != &cxl_port_type)
                return 0;
        port = to_cxl_port(dev);

        device_lock(&port->dev);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dport_dev) {
                        found = 1;
                        break;
                }
        device_unlock(&port->dev);

        return found;
}
Quoted 6 lines

struct cxl_port *find_parent_cxl_port(struct cxl_memdev *cxlmd)

{
        return bus_find_device(&cxl_bus_type, NULL, cxlmd->dev.parent,
                               port_has_dport);
}

Ah, I remember now that I had something like this originally. I thought it may be more desirable to do the grandparent route since you should be able to assert there is a grandparent. I'll change it.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 508

Quoted 5 lines
drivers/cxl/core/bus.c +4 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       port = devm_cxl_add_port(dev, cxl_reg_block(pdev, &map), parent_port);

This is broken because the pci device being used here does not have a driver that knows about CXL bus events.

Quoted 9 lines
Quoted 2 lines

I don't understand this, but I'd like to. Doesn't this make a port device which gets probed by the port driver? Why does the PCI device matter?

I am reacting to the first argument of this call being @dev that came from the pci_dev that was passed in to be the "host" for the devm operation. The devm release action triggers at driver unbind of that host device, but that doesn't make sense because the driver for a switch has nothing to do with CXL operation.

What's the correct host, parent_port->dev?

Quoted 42 lines
Quoted 38 lines

(I'll mention again, switch code is not tested).

Quoted 25 lines
Quoted 25 lines
drivers/cxl/core/bus.c +24 -0
+       put_device(&parent_port->dev);
+       if (IS_ERR(port))
+               dev_err(dev, "Failed to add upstream port %ld\n",
+                       PTR_ERR(port));
+       else
+               dev_dbg(dev, "Added CXL port\n");
+
+       return rc;
+}
+
+static int add_downstream_port(struct pci_dev *pdev)
+{
+       resource_size_t creg = CXL_RESOURCE_NONE;
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       u32 lnkcap, port_num;
+       int rc;
+
+       /*
+        * Ports are to be scanned from top down. Therefore, the upstream port
+        * must already exist.
+        */
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent));
Quoted 9 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 538

Quoted 6 lines
drivers/cxl/core/bus.c +6 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       /*
+        * The spec mandates component registers are present but the
+        * driver does not.

What is this trying to convey?

Quoted 3 lines

That I'm not validating the hardware, and even though component registers are mandatory, the driver will move on even if they're not found. This functionality may need to change in the future and so I left the comment there.

Quoted 6 lines

I think that could be conveyed without comment with something like:

        if (rc)
                creg = CXL_RESOURCE_NONE;
        else
                creg = cxl_reg_block(pdev, &map);

As you like.

#85 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 193 lines · Ben Widawsky
Quoted 191 lines · Dan Williams
Quoted 91 lines · Ben Widawsky
Quoted 72 lines · Dan Williams
Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 
Quoted 16 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

Quoted 4 lines

We had discussed this previously. I was running into an issue when unloading cxl_mem. I needed a way to remove the endpoint port and this was your recommendation. Are you suggesting if the chain is set up correctly, I don't need to do anything?

I think if the chain is set up correctly then you don't need to do anything special. The endpoint port would be devm registered by the cxl_memdev driver to its parent cxl_port provided that port is actively attached to its driver.

Quoted 2 lines

I don't remember exactly what was blowing up but I can try again after things are properly parented.

Cool.

Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
drivers/cxl/core/bus.c +42 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

Quoted 8 lines

The goal is to be able to find the next PCIe port up in the chain.

My understanding was:
pdev = PCIe upstream switch
pdev->dev.parent = PCIe downstream switch connected to pdev. pdev->dev.parent->parent = PCIe upstream switch connected to pdev->dev.parent

I was unable to find an idiomatic way to do that. I'm open to suggestions.

Oh ok, I see it now, but I think this can be done in pure CXL terms and generic devices with the assumption that the parent device of a cxl_memdev must be a dport. Then this works whether the parent port is a platform device like ACPI or cxl_test, or a PCIe device.

static int port_has_dport(struct device *dev, const void *dport_dev)

{
        int found = 0;
        struct cxl_port *port;
        struct cxl_dport *dport;

        if (dev->type != &cxl_port_type)
                return 0;
        port = to_cxl_port(dev);

        device_lock(&port->dev);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dport_dev) {
                        found = 1;
                        break;
                }
        device_unlock(&port->dev);
Quoted 6 lines
Quoted 6 lines
        return found;> > }

struct cxl_port *find_parent_cxl_port(struct cxl_memdev *cxlmd)

{
        return bus_find_device(&cxl_bus_type, NULL, cxlmd->dev.parent,
                               port_has_dport);

Hmm this still needs a grandparent because cxlmd->dev.parent, is the device that registered the memdev and that device's parent is the dport. I need to see if cxl_test supports this property.

Quoted 19 lines
Quoted 2 lines

}

Ah, I remember now that I had something like this originally. I thought it may be more desirable to do the grandparent route since you should be able to assert there is a grandparent. I'll change it.

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 508

Quoted 5 lines
drivers/cxl/core/bus.c +4 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       port = devm_cxl_add_port(dev, cxl_reg_block(pdev, &map), parent_port);

This is broken because the pci device being used here does not have a driver that knows about CXL bus events.

I don't understand this, but I'd like to. Doesn't this make a port device which gets probed by the port driver? Why does the PCI device matter?

Quoted 8 lines
Quoted 6 lines

I am reacting to the first argument of this call being @dev that came from the pci_dev that was passed in to be the "host" for the devm operation. The devm release action triggers at driver unbind of that host device, but that doesn't make sense because the driver for a switch has nothing to do with CXL operation.

What's the correct host, parent_port->dev?

Yeah, provided you have the parent locked and that port is attached to the port driver. However, I'm concerned about lock ordering... I "think" the port driver just needs to make sure that it never takes the parent lock in the remove() path since that's the one that will recurse from an upper layer port ->remove() event.

Quoted 43 lines
Quoted 43 lines
Quoted 42 lines

(I'll mention again, switch code is not tested).

Quoted 25 lines
Quoted 25 lines
drivers/cxl/core/bus.c +24 -0
+       put_device(&parent_port->dev);
+       if (IS_ERR(port))
+               dev_err(dev, "Failed to add upstream port %ld\n",
+                       PTR_ERR(port));
+       else
+               dev_dbg(dev, "Added CXL port\n");
+
+       return rc;
+}
+
+static int add_downstream_port(struct pci_dev *pdev)
+{
+       resource_size_t creg = CXL_RESOURCE_NONE;
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       u32 lnkcap, port_num;
+       int rc;
+
+       /*
+        * Ports are to be scanned from top down. Therefore, the upstream port
+        * must already exist.
+        */
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent));
Quoted 9 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 538

Quoted 6 lines
drivers/cxl/core/bus.c +6 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       /*
+        * The spec mandates component registers are present but the
+        * driver does not.

What is this trying to convey?

That I'm not validating the hardware, and even though component registers are mandatory, the driver will move on even if they're not found. This functionality may need to change in the future and so I left the comment there.

Quoted 8 lines
Quoted 6 lines

I think that could be conveyed without comment with something like:

        if (rc)
                creg = CXL_RESOURCE_NONE;
        else
                creg = cxl_reg_block(pdev, &map);

As you like.

/me tries to resist urge for Princess Bridge reference...

#86 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

In reply to Dan Williams
Quoted 194 lines · Dan Williams
Quoted 91 lines · Ben Widawsky
Quoted 72 lines · Dan Williams
Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 
Quoted 16 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

Quoted 4 lines

We had discussed this previously. I was running into an issue when unloading cxl_mem. I needed a way to remove the endpoint port and this was your recommendation. Are you suggesting if the chain is set up correctly, I don't need to do anything?

I think if the chain is set up correctly then you don't need to do anything special. The endpoint port would be devm registered by the cxl_memdev driver to its parent cxl_port provided that port is actively attached to its driver.

Quoted 2 lines

I don't remember exactly what was blowing up but I can try again after things are properly parented.

Cool.

Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
drivers/cxl/core/bus.c +42 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

Quoted 8 lines

The goal is to be able to find the next PCIe port up in the chain.

My understanding was:
pdev = PCIe upstream switch
pdev->dev.parent = PCIe downstream switch connected to pdev. pdev->dev.parent->parent = PCIe upstream switch connected to pdev->dev.parent

I was unable to find an idiomatic way to do that. I'm open to suggestions.

Oh ok, I see it now, but I think this can be done in pure CXL terms and generic devices with the assumption that the parent device of a cxl_memdev must be a dport. Then this works whether the parent port is a platform device like ACPI or cxl_test, or a PCIe device.

static int port_has_dport(struct device *dev, const void *dport_dev)

{
        int found = 0;
        struct cxl_port *port;
        struct cxl_dport *dport;

        if (dev->type != &cxl_port_type)
                return 0;
        port = to_cxl_port(dev);

        device_lock(&port->dev);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dport_dev) {
                        found = 1;
                        break;
                }
        device_unlock(&port->dev);

        return found;
}
Quoted 6 lines

struct cxl_port *find_parent_cxl_port(struct cxl_memdev *cxlmd)

{
        return bus_find_device(&cxl_bus_type, NULL, cxlmd->dev.parent,
                               port_has_dport);
}

I'm squinting at this and wondering what I will have to change to make it work.

This logic you're asking me to change is the code which is adding the switches to the port hierarchy (before the memdev adds itself as a port). I first need to build that before I can check if the dport matches cxlmd->dev.parent, right?

Since I foolishly added this patch without the consumer, I'll outline here the order.

1. memdev driver walks up hierarchy to find root port & check for switches
2. if parent.driver goto 6 (parent is CXL 2.0 capable root port)
3. starting from the root port this endpoint is connected to, walk down the topology and add all upstream and downstream ports
4. if parent.driver goto 6 (parent is CXL 2.0 DSP)
5. ERROR - don't bind
6. Add self as port

So I'm not seeing how this can work without something like I already have. Please advise. For now, I'll keep my existing logic but call the function find_parent_cxl_port() so we can replace it with something more palatable.

Quoted 66 lines
Quoted 11 lines
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 508

Quoted 5 lines
drivers/cxl/core/bus.c +4 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       port = devm_cxl_add_port(dev, cxl_reg_block(pdev, &map), parent_port);

This is broken because the pci device being used here does not have a driver that knows about CXL bus events.

I don't understand this, but I'd like to. Doesn't this make a port device which gets probed by the port driver? Why does the PCI device matter?

I am reacting to the first argument of this call being @dev that came from the pci_dev that was passed in to be the "host" for the devm operation. The devm release action triggers at driver unbind of that host device, but that doesn't make sense because the driver for a switch has nothing to do with CXL operation.

Quoted 38 lines

(I'll mention again, switch code is not tested).

Quoted 25 lines
Quoted 25 lines
drivers/cxl/core/bus.c +24 -0
+       put_device(&parent_port->dev);
+       if (IS_ERR(port))
+               dev_err(dev, "Failed to add upstream port %ld\n",
+                       PTR_ERR(port));
+       else
+               dev_dbg(dev, "Added CXL port\n");
+
+       return rc;
+}
+
+static int add_downstream_port(struct pci_dev *pdev)
+{
+       resource_size_t creg = CXL_RESOURCE_NONE;
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       u32 lnkcap, port_num;
+       int rc;
+
+       /*
+        * Ports are to be scanned from top down. Therefore, the upstream port
+        * must already exist.
+        */
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent));
Quoted 9 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 538

Quoted 6 lines
drivers/cxl/core/bus.c +6 -0
+       if (!parent_port)
+               return -ENODEV;
+
+       /*
+        * The spec mandates component registers are present but the
+        * driver does not.

What is this trying to convey?

Quoted 3 lines

That I'm not validating the hardware, and even though component registers are mandatory, the driver will move on even if they're not found. This functionality may need to change in the future and so I left the comment there.

I think that could be conveyed without comment with something like:

        if (rc)
                creg = CXL_RESOURCE_NONE;
        else
                creg = cxl_reg_block(pdev, &map);
#87 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 220 lines · Ben Widawsky
Quoted 194 lines · Dan Williams
Quoted 91 lines · Ben Widawsky
Quoted 72 lines · Dan Williams
Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 
Quoted 16 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

Quoted 4 lines

We had discussed this previously. I was running into an issue when unloading cxl_mem. I needed a way to remove the endpoint port and this was your recommendation. Are you suggesting if the chain is set up correctly, I don't need to do anything?

I think if the chain is set up correctly then you don't need to do anything special. The endpoint port would be devm registered by the cxl_memdev driver to its parent cxl_port provided that port is actively attached to its driver.

Quoted 2 lines

I don't remember exactly what was blowing up but I can try again after things are properly parented.

Cool.

Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
drivers/cxl/core/bus.c +42 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

Quoted 8 lines

The goal is to be able to find the next PCIe port up in the chain.

My understanding was:
pdev = PCIe upstream switch
pdev->dev.parent = PCIe downstream switch connected to pdev. pdev->dev.parent->parent = PCIe upstream switch connected to pdev->dev.parent

I was unable to find an idiomatic way to do that. I'm open to suggestions.

Oh ok, I see it now, but I think this can be done in pure CXL terms and generic devices with the assumption that the parent device of a cxl_memdev must be a dport. Then this works whether the parent port is a platform device like ACPI or cxl_test, or a PCIe device.

static int port_has_dport(struct device *dev, const void *dport_dev)

{
        int found = 0;
        struct cxl_port *port;
        struct cxl_dport *dport;

        if (dev->type != &cxl_port_type)
                return 0;
        port = to_cxl_port(dev);

        device_lock(&port->dev);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dport_dev) {
                        found = 1;
                        break;
                }
        device_unlock(&port->dev);

        return found;
}
Quoted 6 lines

struct cxl_port *find_parent_cxl_port(struct cxl_memdev *cxlmd)

{
        return bus_find_device(&cxl_bus_type, NULL, cxlmd->dev.parent,
                               port_has_dport);
}

I'm squinting at this and wondering what I will have to change to make it work.

This logic you're asking me to change is the code which is adding the switches to the port hierarchy (before the memdev adds itself as a port). I first need to build that before I can check if the dport matches cxlmd->dev.parent, right?

Since I foolishly added this patch without the consumer, I'll outline here the order.

1. memdev driver walks up hierarchy to find root port & check for switches
2. if parent.driver goto 6 (parent is CXL 2.0 capable root port)
3. starting from the root port this endpoint is connected to, walk down the topology and add all upstream and downstream ports
4. if parent.driver goto 6 (parent is CXL 2.0 DSP)
5. ERROR - don't bind
6. Add self as port

Quoted 3 lines

So I'm not seeing how this can work without something like I already have. Please advise. For now, I'll keep my existing logic but call the function find_parent_cxl_port() so we can replace it with something more palatable.

I am assuming that the grandparent of a memdev must be a dport, and the grandparent of a dport must be another dport (until the root is reached). With that assumption the topology can be built up incrementally without needing to explicitly verify PCIE port types. Something like the following where @dev is the endpoint memdev.

        while (dport = grandparent(dev)) {
                if (!is_cxl_dport(dport))
                        if (is_cxl_dport(grandparent(dport)))
                                /* add a new cxl_port */;
                        else {
                                /* walk up and retry */;
                                continue;
                        }
                port = dport->port;
                if (port->dev.driver)
                        /* good to go, CXL.mem is enabled all the way
to @dport */
                else
                        /* CXL.mem is disabled from @dport down */
        }

...where that needs repeating until it either finds a disabled cxl_port, or until it stops adding new cxl_ports. Does that match your proposal?

#88 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 247 lines · Dan Williams
Quoted 220 lines · Ben Widawsky
Quoted 194 lines · Dan Williams
Quoted 91 lines · Ben Widawsky
Quoted 72 lines · Dan Williams
Quoted 71 lines · Ben Widawsky

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c
Documentation/driver-api/cxl/memory-devices.rst +6 -0
index fbf0393cdddc..547336c95593 100644
@@ -47,6 +47,12 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/bus.c
    :identifiers:
 
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :doc: cxl pci
+
+.. kernel-doc:: drivers/cxl/core/pci.c
+   :identifiers:
+
 .. kernel-doc:: drivers/cxl/core/pmem.c
    :doc: cxl pmem
 
drivers/cxl/core/Makefile +1 -0
index 07eb8e1fb8a6..9d33d2d5bf09 100644
@@ -7,3 +7,4 @@ cxl_core-y += pmem.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
+cxl_core-y += pci.o
drivers/cxl/core/bus.c +1 -0
index c7e1894d503b..f10e7d5b22a4 100644
@@ -8,6 +8,7 @@
 #include <linux/idr.h>
 #include <cxlmem.h>
 #include <cxl.h>
+#include <pci.h>
 #include "core.h"
 
 /**
@@ -445,6 +446,150 @@ struct cxl_port *devm_cxl_add_port(struct device *uport,
 }
 EXPORT_SYMBOL_GPL(devm_cxl_add_port);
 
Quoted 16 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 458

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+void devm_cxl_remove_port(struct cxl_port *port)
+{
+       down_read(&root_host_sem);
+       if (cxl_root_host) {
+               devm_release_action(cxl_root_host, cxl_unlink_uport, port);
+               devm_release_action(cxl_root_host, unregister_port, port);
+       }
+       up_read(&root_host_sem);
+}
+EXPORT_SYMBOL_GPL(devm_cxl_remove_port);

If the scan establishes the property that all child ports are devm allocated with their cxl_port-parent, and only if the cxl_port-parent is bound to its driver then I think we don't need to play devm_release_action games().

Quoted 4 lines

We had discussed this previously. I was running into an issue when unloading cxl_mem. I needed a way to remove the endpoint port and this was your recommendation. Are you suggesting if the chain is set up correctly, I don't need to do anything?

I think if the chain is set up correctly then you don't need to do anything special. The endpoint port would be devm registered by the cxl_memdev driver to its parent cxl_port provided that port is actively attached to its driver.

Quoted 2 lines

I don't remember exactly what was blowing up but I can try again after things are properly parented.

Cool.

Quoted 52 lines
Quoted 43 lines
Quoted 43 lines
drivers/cxl/core/bus.c +42 -0
+
+static int match_port(struct device *dev, const void *data)
+{
+       struct pci_dev *pdev = (struct pci_dev *)data;
+
+       if (dev->type != &cxl_port_type)
+               return 0;
+
+       return to_cxl_port(dev)->uport == &pdev->dev;
+}
+
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+       struct device *port_dev;
+
+       if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+               return NULL;
+
+       port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+       if (port_dev)
+               return to_cxl_port(port_dev);
+
+       return NULL;
+}
+
+static int add_upstream_port(struct device *host, struct pci_dev *pdev)
+{
+       struct device *dev = &pdev->dev;
+       struct cxl_port *parent_port;
+       struct cxl_register_map map;
+       struct cxl_port *port;
+       int rc;
+
+       /*
+        * Upstream ports must be connected to a downstream port or root port.
+        * That downstream or root port must have a parent.
+        */
+       if (!pdev->dev.parent->parent)
+               return -ENXIO;
+
+       /* A port is useless if there are no component registers */
+       rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
Quoted 8 lines

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 504

Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+       if (rc)
+               return rc;
+
+       parent_port = find_cxl_port(to_pci_dev(pdev->dev.parent->parent));

This deref chain is unreadable. It wants a helper if it stays, but I can't immediately think of a reason to ever need to look at a grandparent in the hierarchy.

Quoted 8 lines

The goal is to be able to find the next PCIe port up in the chain.

My understanding was:
pdev = PCIe upstream switch
pdev->dev.parent = PCIe downstream switch connected to pdev. pdev->dev.parent->parent = PCIe upstream switch connected to pdev->dev.parent

I was unable to find an idiomatic way to do that. I'm open to suggestions.

Oh ok, I see it now, but I think this can be done in pure CXL terms and generic devices with the assumption that the parent device of a cxl_memdev must be a dport. Then this works whether the parent port is a platform device like ACPI or cxl_test, or a PCIe device.

static int port_has_dport(struct device *dev, const void *dport_dev)

{
        int found = 0;
        struct cxl_port *port;
        struct cxl_dport *dport;

        if (dev->type != &cxl_port_type)
                return 0;
        port = to_cxl_port(dev);

        device_lock(&port->dev);
        list_for_each_entry (dport, &port->dports, list)
                if (dport->dport == dport_dev) {
                        found = 1;
                        break;
                }
        device_unlock(&port->dev);

        return found;
}
Quoted 6 lines

struct cxl_port *find_parent_cxl_port(struct cxl_memdev *cxlmd)

{
        return bus_find_device(&cxl_bus_type, NULL, cxlmd->dev.parent,
                               port_has_dport);
}

I'm squinting at this and wondering what I will have to change to make it work.

This logic you're asking me to change is the code which is adding the switches to the port hierarchy (before the memdev adds itself as a port). I first need to build that before I can check if the dport matches cxlmd->dev.parent, right?

Since I foolishly added this patch without the consumer, I'll outline here the order.

1. memdev driver walks up hierarchy to find root port & check for switches
2. if parent.driver goto 6 (parent is CXL 2.0 capable root port)
3. starting from the root port this endpoint is connected to, walk down the topology and add all upstream and downstream ports
4. if parent.driver goto 6 (parent is CXL 2.0 DSP)
5. ERROR - don't bind
6. Add self as port

Quoted 3 lines

So I'm not seeing how this can work without something like I already have. Please advise. For now, I'll keep my existing logic but call the function find_parent_cxl_port() so we can replace it with something more palatable.

I am assuming that the grandparent of a memdev must be a dport, and the grandparent of a dport must be another dport (until the root is reached). With that assumption the topology can be built up incrementally without needing to explicitly verify PCIE port types. Something like the following where @dev is the endpoint memdev.

        while (dport = grandparent(dev)) {
                if (!is_cxl_dport(dport))
                        if (is_cxl_dport(grandparent(dport)))
                                /* add a new cxl_port */;
                        else {
                                /* walk up and retry */;
                                continue;
                        }
                port = dport->port;
                if (port->dev.driver)
                        /* good to go, CXL.mem is enabled all the way
to @dport */
                else
                        /* CXL.mem is disabled from @dport down */
        }
Quoted 3 lines

...where that needs repeating until it either finds a disabled cxl_port, or until it stops adding new cxl_ports. Does that match your proposal?

I think I'm not conveying something properly, or I'm not fully understanding you. The dports can only be added as you enumerate switches (assuming the endpoint isn't directly connected to the root port). I'm specifically referring to the enumeration part of the code where the switch port/dports are added. This comes after the memdev driver walks up to find there is a switch in the middle.

I've done some cleanup, let me resend and we can further discuss on that. I think it will be clearer with the last two patches here combined.

#89 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

In reply to Ben Widawsky
Quoted 8 lines · On Fri, 22 Oct 2021 11:36:56 -0700 Ben Widawsky <[email protected]> wrote:

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

Quoted 8 lines

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

This is an unusual enough thing to be doing on PCI that I'd suggest making sure to cc linux-pci + Bjorn for next version of this... Shall we say, this makes me nervous and more eyes might be good :)

One trivial inline.

Quoted 12 lines
---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c

...

Quoted 4 lines
drivers/cxl/core/bus.c +0 -0
index c7e1894d503b..f10e7d5b22a4 100644

...

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 479

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+	struct device *port_dev;
+
+	if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+		return NULL;
+
+	port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+	if (port_dev)
+		return to_cxl_port(port_dev);

Flip this logic to make it more readable.

	if (!port_dev)
		return NULL;
Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+
+	return NULL;
+}
+
#90 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 18 lines · Jonathan Cameron
Quoted 15 lines · On Fri, 22 Oct 2021 11:36:56 -0700 Ben Widawsky <[email protected]> wrote:

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

Quoted 7 lines
Quoted 1 line

This is an unusual enough thing to be doing on PCI that I'd suggest making sure to cc linux-pci + Bjorn for next version of this... Shall we say, this makes me nervous and more eyes might be good :)

One trivial inline.

Makes sense, just this patch or the whole series?

Quoted 38 lines
Quoted 12 lines
---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c

...

Quoted 4 lines
drivers/cxl/core/bus.c +0 -0
index c7e1894d503b..f10e7d5b22a4 100644

...

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 479

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+	struct device *port_dev;
+
+	if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+		return NULL;
+
+	port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+	if (port_dev)
+		return to_cxl_port(port_dev);

Flip this logic to make it more readable.

	if (!port_dev)
		return NULL;
Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+
+	return NULL;
+}
+
#91 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 21 lines · On Wed, 10 Nov 2021 09:49:23 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 20 lines · Jonathan Cameron
Quoted 16 lines · On Fri, 22 Oct 2021 11:36:56 -0700 Ben Widawsky <[email protected]> wrote:

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

Quoted 7 lines
Quoted 5 lines

This is an unusual enough thing to be doing on PCI that I'd suggest making sure to cc linux-pci + Bjorn for next version of this... Shall we say, this makes me nervous and more eyes might be good :)

One trivial inline.

Makes sense, just this patch or the whole series?

The nervousness was just this patch IIRC.

Jonathan

Quoted 39 lines
Quoted 39 lines
Quoted 13 lines
---
 .../driver-api/cxl/memory-devices.rst         |   6 +
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        | 145 ++++++++++++++++++
 drivers/cxl/core/pci.c                        |  99 ++++++++++++
 drivers/cxl/cxl.h                             |   2 +
 drivers/cxl/pci.h                             |   6 +
 drivers/cxl/port.c                            |   2 +-
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 261 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cxl/core/pci.c

...

Quoted 4 lines
drivers/cxl/core/bus.c +0 -0
index c7e1894d503b..f10e7d5b22a4 100644

...

drivers/cxl/core/bus.c struct cxl_port *devm_cxl_add_port(struct device *uport, · line 479

Quoted 10 lines
drivers/cxl/core/bus.c +10 -0
+static struct cxl_port *find_cxl_port(struct pci_dev *usp)
+{
+	struct device *port_dev;
+
+	if (!pci_is_pcie(usp) || pci_pcie_type(usp) != PCI_EXP_TYPE_UPSTREAM)
+		return NULL;
+
+	port_dev = bus_find_device(&cxl_bus_type, NULL, usp, match_port);
+	if (port_dev)
+		return to_cxl_port(port_dev);  

Flip this logic to make it more readable.

	if (!port_dev)
		return NULL;
Quoted 4 lines
drivers/cxl/core/bus.c +4 -0
+
+	return NULL;
+}
+  
#92 lore raw

Re: [RFC PATCH v2 15/28] cxl/core: Introduce API to scan switch ports

Quoted 29 lines · Jonathan Cameron
Quoted 25 lines · On Wed, 10 Nov 2021 09:49:23 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 24 lines · Jonathan Cameron
Quoted 8 lines · On Fri, 22 Oct 2021 11:36:56 -0700 Ben Widawsky <[email protected]> wrote:

The CXL drivers encapsulate the components that direct memory traffic in an entity known as a cxl_port. Compute Express Link specifies three such
components: hostbridge (ie. a collection of root ports), switches, and endpoints. There are currently drivers that create these ports for the hostbridges and the endpoints (cxl_acpi and cxl_mem). The new API introduced allows callers to initiate a scan down from the hostbridge and create ports for switches in the CXL topology.

Quoted 8 lines

The intended user of this API is for endpoint devices. An endpoint device will need to determine if it is CXL.mem capable, which requires all components in the path from hostbridge to the endpoint to be CXL.mem capable. Once an endpoint device determines it's connected to a CXL capable root port, it can call this API to fill in all the ports in between the hostbridge and itself.

This is an unusual enough thing to be doing on PCI that I'd suggest making sure to cc linux-pci + Bjorn for next version of this... Shall we say, this makes me nervous and more eyes might be good :)

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

One trivial inline.

Makes sense, just this patch or the whole series?

The nervousness was just this patch IIRC.

I would copy linux-pci on the whole thing. The parallel universe that drivers/cxl/ needs and is attaching to PCI objects in the device tree should at least be something PCI developers are aware of, especially when we come back to talk about Native PCI hotplug interactions with CXL.

#93 lore raw

RFC PATCH v2 16/28 cxl: Introduce cxl_mem driver

In reply to Ben Widawsky

Add a driver that is capable of determining whether a device is in a CXL.mem routed part of the topology.

This driver allows a higher level driver - such as one controlling CXL regions, which is itself a set of CXL devices - to easily determine if the CXL devices are CXL.mem capable by checking if the driver has bound. CXL memory device services may also be provided by this driver though none are needed as of yet. cxl_mem also plays the part of registering itself as an endpoint port, which is a required step to enumerate the device's HDM decoder resources.

As part of this patch, find_dport_by_dev() is promoted to the cxl_core's set of APIs for use by the new driver.

---
 .../driver-api/cxl/memory-devices.rst         |   3 +
 drivers/cxl/Kconfig                           |  15 ++
 drivers/cxl/Makefile                          |   2 +
 drivers/cxl/acpi.c                            |  17 +-
 drivers/cxl/core/bus.c                        |  19 +++
 drivers/cxl/core/core.h                       |   1 +
 drivers/cxl/core/memdev.c                     |   2 +-
 drivers/cxl/cxl.h                             |   3 +
 drivers/cxl/cxlmem.h                          |   2 +
 drivers/cxl/mem.c                             | 158 ++++++++++++++++++
 10 files changed, 205 insertions(+), 17 deletions(-)
 create mode 100644 drivers/cxl/mem.c
Documentation/driver-api/cxl/memory-devices.rst +3 -0
index 547336c95593..d04d07ae7118 100644
@@ -28,6 +28,9 @@ CXL Memory Device
 .. kernel-doc:: drivers/cxl/pci.c
    :internal:
 
+.. kernel-doc:: drivers/cxl/mem.c
+   :doc: cxl mem
+
 CXL Port
 --------
 .. kernel-doc:: drivers/cxl/port.c
drivers/cxl/Kconfig +15 -0
index 23773d0ac896..bce98bdaf12c 100644
@@ -28,6 +28,21 @@ config CXL_PCI
 
 	  If unsure say 'm'.
 
+config CXL_MEM
+	tristate "CXL.mem: Memory Devices"
+	default CXL_BUS
+        help
+          The CXL.mem protocol allows a device to act as a provider of
+	  "System RAM" and/or "Persistent Memory" that is fully coherent
+	  as if the memory was attached to the typical CPU memory controller.
+	  This is known as HDM "Host-managed Device Memory".
+
+	  Say 'y/m' to enable a driver that will attach to CXL.mem devices for
+	  memory expansion and control of HDM. See Chapter 9.13 in the CXL 2.0
+	  specification for a detailed description of HDM.
+
+	  If unsure say 'm'.
+
 config CXL_MEM_RAW_COMMANDS
 	bool "RAW Command Interface for Memory Devices"
 	depends on CXL_PCI
drivers/cxl/Makefile +2 -0
index 40b386aaedf7..77499a6b40f2 100644
@@ -2,9 +2,11 @@
 obj-$(CONFIG_CXL_BUS) += core/
 obj-$(CONFIG_CXL_MEM) += cxl_port.o
 obj-$(CONFIG_CXL_PCI) += cxl_pci.o
+obj-$(CONFIG_CXL_MEM) += cxl_mem.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
 
+cxl_mem-y := mem.o
 cxl_pci-y := pci.o
 cxl_acpi-y := acpi.o
 cxl_pmem-y := pmem.o
drivers/cxl/acpi.c +1 -16
index f6b78736c933..23afb3b8f5cc 100644
@@ -243,21 +243,6 @@ __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
 	return 0;
 }
 
-static struct cxl_dport *find_dport_by_dev(struct cxl_port *port, struct device *dev)
-{
-	struct cxl_dport *dport;
-
-	device_lock(&port->dev);
-	list_for_each_entry(dport, &port->dports, list)
-		if (dport->dport == dev) {
-			device_unlock(&port->dev);
-			return dport;
-		}
-
-	device_unlock(&port->dev);
-	return NULL;
-}
-
 __mock struct acpi_device *to_cxl_host_bridge(struct device *host,
 					      struct device *dev)
 {
@@ -292,7 +277,7 @@ static int add_host_bridge_uport(struct device *match, void *arg)
 	if (!bridge)
 		return 0;
 
-	dport = find_dport_by_dev(root_port, match);
+	dport = cxl_find_dport_by_dev(root_port, match);
 	if (!dport) {
 		dev_dbg(host, "host bridge expected and not found\n");
 		return -ENODEV;
drivers/cxl/core/bus.c +19 -0
index f10e7d5b22a4..4b4e6033b774 100644
@@ -673,6 +673,23 @@ int cxl_add_dport(struct cxl_port *port, struct device *dport_dev, int port_id,
 }
 EXPORT_SYMBOL_GPL(cxl_add_dport);
 
+struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port,
+					struct device *dev)
+{
+	struct cxl_dport *dport;
+
+	device_lock(&port->dev);
+	list_for_each_entry(dport, &port->dports, list)
+		if (dport->dport == dev) {
+			device_unlock(&port->dev);
+			return dport;
+		}
+
+	device_unlock(&port->dev);
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(cxl_find_dport_by_dev);
+
 static int decoder_populate_targets(struct cxl_decoder *cxld,
 				    struct cxl_port *port, int *target_map)
 {
@@ -859,6 +876,8 @@ static int cxl_device_id(struct device *dev)
 		return CXL_DEVICE_NVDIMM;
 	if (dev->type == &cxl_port_type)
 		return CXL_DEVICE_PORT;
+	if (dev->type == &cxl_memdev_type)
+		return CXL_DEVICE_MEMORY_EXPANDER;
 	return 0;
 }
 
drivers/cxl/core/core.h +1 -0
index e0c9aacc4e9c..dea246cb7c58 100644
@@ -6,6 +6,7 @@
 
 extern const struct device_type cxl_nvdimm_bridge_type;
 extern const struct device_type cxl_nvdimm_type;
+extern const struct device_type cxl_memdev_type;
 
 extern struct attribute_group cxl_base_attribute_group;
 
drivers/cxl/core/memdev.c +1 -1
index 15762c16d83f..4a7a6e48cba2 100644
@@ -127,7 +127,7 @@ static const struct attribute_group *cxl_memdev_attribute_groups[] = {
 	NULL,
 };
 
-static const struct device_type cxl_memdev_type = {
+const struct device_type cxl_memdev_type = {
 	.name = "cxl_memdev",
 	.release = cxl_memdev_release,
 	.devnode = cxl_memdev_devnode,
drivers/cxl/cxl.h +3 -0
index 03b414462416..5823c267a4de 100644
@@ -315,6 +315,8 @@ void cxl_scan_ports(struct cxl_dport *root_port);
 int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
 		  resource_size_t component_reg_phys, bool root_port);
 struct cxl_dport *cxl_get_root_dport(struct device *dev);
+struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port,
+					struct device *dev);
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
@@ -350,6 +352,7 @@ void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 #define CXL_DEVICE_NVDIMM_BRIDGE	1
 #define CXL_DEVICE_NVDIMM		2
 #define CXL_DEVICE_PORT			3
+#define CXL_DEVICE_MEMORY_EXPANDER	4
 
 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
 #define CXL_MODALIAS_FMT "cxl:t%d"
drivers/cxl/cxlmem.h +2 -0
index 62fe8e2c59e4..cc5844150ce0 100644
@@ -36,6 +36,7 @@
  * @cxlm: pointer to the parent device driver data
  * @id: id number of this memdev instance.
  * @creg_base: register base of component registers
+ * @root_port: Hostbridge's root port connected to this endpoint
  */
 struct cxl_memdev {
 	struct device dev;
@@ -43,6 +44,7 @@ struct cxl_memdev {
 	struct cxl_mem *cxlm;
 	int id;
 	resource_size_t creg_base;
+	struct cxl_dport *root_port;
 };
 
 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
drivers/cxl/mem.c new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
2.33.1
#94 lore raw

RFC PATCH v2 17/28 cxl: Disable switch hierarchies for now

In reply to Ben Widawsky

Switches aren't supported by the region driver yet. If a device finds itself under a switch it will not bind a driver so that it cannot be used later for region creation/configuration.

---
 drivers/cxl/mem.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/cxl/mem.c +5 -0
index 97092b9b748d..0b0272576e55 100644
@@ -121,6 +121,11 @@ static int cxl_mem_probe(struct device *dev)
 		goto out;
 	}
 
+	/* FIXME: Add true switch support */
+	dev_err(dev, "Devices behind switches are currently unsupported\n");
+	rc = -ENODEV;
+	goto err_out;
+
 	/* Walk down from the root port and add all switches */
 	cxl_scan_ports(ctx.root_port);
 
Signature · 1 line
2.33.1
#95 lore raw

RFC PATCH v2 18/28 cxl/region: Add region creation ABI

In reply to Ben Widawsky

Regions are created as a child of the decoder that encompasses an address space with constraints. Regions have a number of attributes that must be configured before the region can be activated.

The ABI is not meant to be secure, but is meant to avoid accidental races. As a result, a buggy process may create a region by name that was allocated by a different process. However, multiple processes which are trying not to race with each other shouldn't need special synchronization to do so.

// Allocate a new region name
region = $(cat /sys/bus/cxl/devices/decoder0.0/create_region)

// Create a new region by name
echo $region > /sys/bus/cxl/devices/decoder0.0/create_region

// Region now exists in sysfs
stat -t /sys/bus/cxl/devices/decoder0.0/$region

// Delete the region, and name
echo $region > /sys/bus/cxl/devices/decoder0.0/delete_region

After creating a region, it will show up in sysfs

/sys/bus/cxl/devices/

├── decoder0.0 -> ../../../devices/platform/ACPI0017:00/root0/decoder0.0
├── decoder1.0 -> ../../../devices/platform/ACPI0017:00/root0/port1/decoder1.0
├── decoder2.0 -> ../../../devices/platform/ACPI0017:00/root0/port1/port2/decoder2.0
├── mem0 -> ../../../devices/pci0000:34/0000:34:00.0/0000:35:00.0/mem0
├── pmem0 -> ../../../devices/pci0000:34/0000:34:00.0/0000:35:00.0/mem0/pmem0
├── port1 -> ../../../devices/platform/ACPI0017:00/root0/port1
├── port2 -> ../../../devices/platform/ACPI0017:00/root0/port1/port2
└── root0 -> ../../../devices/platform/ACPI0017:00/root0
---
 Documentation/ABI/testing/sysfs-bus-cxl       |  23 +++
 .../driver-api/cxl/memory-devices.rst         |  11 ++
 drivers/cxl/core/Makefile                     |   1 +
 drivers/cxl/core/bus.c                        |  84 +++++++++++
 drivers/cxl/core/region.c                     | 139 ++++++++++++++++++
 drivers/cxl/cxl.h                             |   9 ++
 drivers/cxl/region.h                          |  37 +++++
 tools/testing/cxl/Kbuild                      |   1 +
 8 files changed, 305 insertions(+)
 create mode 100644 drivers/cxl/core/region.c
 create mode 100644 drivers/cxl/region.h
Documentation/ABI/testing/sysfs-bus-cxl +23 -0
index 0b6a2e6e8fbb..5c48d2ed108f 100644
@@ -127,3 +127,26 @@ Description:
 		memory (type-3). The 'target_type' attribute indicates the
 		current setting which may dynamically change based on what
 		memory regions are activated in this decode hierarchy.
+
+What:		/sys/bus/cxl/devices/decoderX.Y/create_region
+Date:		June, 2021
+KernelVersion:	v5.16
+Contact:	[email protected]
+Description:
+		Creates a new CXL region. Writing a value of the form
+		"regionX.Y:Z" will create a new uninitialized region that will
+		be mapped by the CXL decoderX.Y. Reading from this node will
+		return a newly allocated region name. In order to create a
+		region (writing) you must use a value returned from reading the
+		node. Regions must be subsequently configured and bound to a
+		region driver before they can be used.
+
+What:		/sys/bus/cxl/devices/decoderX.Y/delete_region
+Date:		June, 2021
+KernelVersion:	v5.16
+Contact:	[email protected]
+Description:
+		Deletes the named region. A region must be unbound from the
+		region driver before being deleted. The attributes expects a
+		region in the form "regionX.Y:Z". The region's name, allocated
+		by reading create_region, will also be released.
Documentation/driver-api/cxl/memory-devices.rst +11 -0
index d04d07ae7118..0ed00906acdd 100644
@@ -65,6 +65,17 @@ CXL Core
 .. kernel-doc:: drivers/cxl/core/mbox.c
    :doc: cxl mbox
 
+CXL Regions
+-----------
+.. kernel-doc:: drivers/cxl/region.h
+   :identifiers:
+
+.. kernel-doc:: drivers/cxl/core/region.c
+   :doc: cxl core region
+
+.. kernel-doc:: drivers/cxl/core/region.c
+   :identifiers:
+
 External Interfaces
 ===================
 
drivers/cxl/core/Makefile +1 -0
index 9d33d2d5bf09..6a3b3ecd7a25 100644
@@ -4,6 +4,7 @@ obj-$(CONFIG_CXL_BUS) += cxl_core.o
 ccflags-y += -DDEFAULT_SYMBOL_NAMESPACE=CXL -I$(srctree)/drivers/cxl
 cxl_core-y := bus.o
 cxl_core-y += pmem.o
+cxl_core-y += region.o
 cxl_core-y += regs.o
 cxl_core-y += memdev.o
 cxl_core-y += mbox.o
drivers/cxl/core/bus.c +84 -0

113 lines not on this page · raw message

drivers/cxl/core/region.c new file +139 -0

139 lines not on this page · raw message

drivers/cxl/cxl.h +9 -0
index 5823c267a4de..bcef2cb4e55b 100644
@@ -220,6 +220,7 @@ enum cxl_decoder_type {
  * @interleave_granularity: data stride per dport
  * @target_type: accelerator vs expander (type2 vs type3) selector
  * @flags: memory type capabilities and locking
+ * @region_ida: allocator for region ids.
  * @nr_targets: number of elements in @target
  * @target: active ordered target list in current decoder configuration
  */
@@ -231,6 +232,7 @@ struct cxl_decoder {
 	int interleave_granularity;
 	enum cxl_decoder_type target_type;
 	unsigned long flags;
+	struct ida region_ida;
 	const int nr_targets;
 	struct cxl_dport *target[];
 };
@@ -305,6 +307,13 @@ struct cxl_dport {
 	struct list_head root_port_link;
 };
 
+bool is_cxl_region(struct device *dev);
+struct cxl_region *to_cxl_region(struct device *dev);
+struct cxl_region *cxl_alloc_region(struct cxl_decoder *cxld,
+				    int interleave_ways);
+int cxl_add_region(struct cxl_decoder *cxld, struct cxl_region *region);
+int cxl_delete_region(struct cxl_decoder *cxld, const char *region);
+
 struct cxl_port *to_cxl_port(struct device *dev);
 struct cxl_port *devm_cxl_add_port(struct device *uport,
 				   resource_size_t component_reg_phys,
drivers/cxl/region.h new file +37 -0
new file mode 100644
index 000000000000..d032df438832
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright(c) 2021 Intel Corporation. */
+#ifndef __CXL_REGION_H__
+#define __CXL_REGION_H__
+
+#include <linux/uuid.h>
+
+#include "cxl.h"
+
+/**
+ * struct cxl_region - CXL region
+ * @dev: This region's device.
+ * @id: This regions id. Id is globally unique across all regions.
+ * @list: Node in decoder's region list.
+ * @res: Address space consumed by this region.
+ * @size: Size of the region determined from LSA or userspace.
+ * @uuid: The UUID for this region.
+ * @eniw: Number of interleave ways this region is configured for.
+ * @ig: Interleave granularity of region
+ * @targets: The memory devices comprising the region.
+ */
+struct cxl_region {
+	struct device dev;
+	int id;
+	struct list_head list;
+
+	struct {
+		struct resource *res;
+		u64 size;
+		uuid_t uuid;
+		int eniw;
+		int ig;
+		struct cxl_memdev *targets[CXL_DECODER_MAX_INTERLEAVE];
+	};
+};
+
+#endif
tools/testing/cxl/Kbuild +1 -0
index 46db4dd345a0..3e5a1c4dce14 100644
@@ -32,6 +32,7 @@ cxl_core-y += $(CXL_CORE_SRC)/regs.o
 cxl_core-y += $(CXL_CORE_SRC)/memdev.o
 cxl_core-y += $(CXL_CORE_SRC)/mbox.o
 cxl_core-y += $(CXL_CORE_SRC)/pci.o
+cxl_core-y += $(CXL_CORE_SRC)/region.o
 cxl_core-y += config_check.o
 
 cxl_core-y += mock_pmem.o
Signature · 1 line
2.33.1
#96 lore raw

RFC PATCH v2 19/28 cxl/region: Introduce concept of region configuration

In reply to Ben Widawsky

The region creation APIs create a vacant region. Configuring the region works in the same way as similar subsystems such as devdax. Sysfs attrs will be provided to allow userspace to configure the region. Finally once all configuration is complete, userspace may activate the region.

Introduced here are the most basic attributes needed to configure a region. Details of these attribute are described in the ABI Documentation.

A example is provided below:

/sys/bus/cxl/devices/region0.0:0

├── interleave_granularity
├── interleave_ways
├── offset
├── size
├── subsystem -> ../../../../../../bus/cxl
├── target0
├── uevent
└── uuid
---
 Documentation/ABI/testing/sysfs-bus-cxl |  40 ++++
 drivers/cxl/core/region.c               | 295 ++++++++++++++++++++++++
 2 files changed, 335 insertions(+)
Documentation/ABI/testing/sysfs-bus-cxl +40 -0
index 5c48d2ed108f..ef9b10d05c59 100644
@@ -150,3 +150,43 @@ Description:
 		region driver before being deleted. The attributes expects a
 		region in the form "regionX.Y:Z". The region's name, allocated
 		by reading create_region, will also be released.
+
+What:		/sys/bus/cxl/devices/decoderX.Y/regionX.Y:Z/offset
+Date:		June, 2021
+KernelVersion:	v5.16
+Contact:	[email protected]
+Description:
+		(RO) A region resides within an address space that is claimed by
+		a decoder. Region space allocation is handled by the driver, but
+		the offset may be read by userspace tooling in order to
+		determine fragmentation, and available size for new regions.
+
+What:
+/sys/bus/cxl/devices/decoderX.Y/regionX.Y:Z/{interleave,size,uuid,target[0-15]}
+Date:		June, 2021
+KernelVersion:	v5.16
+Contact:	[email protected]
+Description:
+		(RW) Configuring regions requires a minimal set of parameters in
+		order for the subsequent bind operation to succeed. The
+		following parameters are defined:
+
+		==	========================================================
+		interleave_granularity Mandatory. Number of consecutive bytes
+			each device in the interleave set will claim. The
+			possible interleave granularity values are determined by
+			the CXL spec and the participating devices.
+		interleave_ways Mandatory. Number of devices participating in the
+			region. Each device will provide 1/interleave of storage
+			for the region.
+		size	Manadatory. Phsyical address space the region will
+			consume.
+		target  Mandatory. Memory devices are the backing storage for a
+			region. There will be N targets based on the number of
+			interleave ways that the top level decoder is configured
+			for. Each target must be set with a memdev device ie.
+			'mem1'. This attribute only becomes available after
+			setting the 'interleave' attribute.
+		uuid	Optional. A unique identifier for the region. If none is
+			selected, the kernel will create one.
+		==	========================================================
drivers/cxl/core/region.c +295 -0

323 lines not on this page · raw message

Signature · 1 line
2.33.1
#97 lore raw

Re: [RFC PATCH v2 19/28] cxl/region: Introduce concept of region configuration

Quoted 21 lines · On Fri, 22 Oct 2021 11:37:00 -0700 Ben Widawsky <[email protected]> wrote:

The region creation APIs create a vacant region. Configuring the region works in the same way as similar subsystems such as devdax. Sysfs attrs will be provided to allow userspace to configure the region. Finally once all configuration is complete, userspace may activate the region.

Introduced here are the most basic attributes needed to configure a region. Details of these attribute are described in the ABI Documentation.

A example is provided below:

/sys/bus/cxl/devices/region0.0:0

├── interleave_granularity
├── interleave_ways
├── offset
├── size
├── subsystem -> ../../../../../../bus/cxl
├── target0
├── uevent
└── uuid
Quoted 2 lines

---

Not a proper review, but just one thing noticed whilst messing around with this.

Quoted 26 lines
drivers/cxl/core/region.c +13 -0
index 588f0ca65bb2..3b0d74d4dd6c 100644
@@ -3,9 +3,12 @@
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/device.h>
 #include <linux/module.h>
+#include <linux/sizes.h>
 #include <linux/slab.h>
+#include <linux/uuid.h>
 #include <linux/idr.h>
 #include <region.h>
+#include <cxlmem.h>
 #include <cxl.h>
 
+
+static size_t set_targetN(struct cxl_region *region, const char *buf, int n,
+			  size_t len)
+{
+	struct device *memdev_dev;
+	struct cxl_memdev *cxlmd;
+
+	device_lock(&region->dev);
+
+	if (len == 1 || region->targets[n])

drivers/cxl/core/region.c

Quoted 10 lines
drivers/cxl/core/region.c +10 -0
+		remove_target(region, n);
+
+	/* Remove target special case */
+	if (len == 1) {
+		device_unlock(&region->dev);
+		return len;
+	}
+
+	memdev_dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf);
+	if (!memdev_dev)

device_unlock()?

drivers/cxl/core/region.c line 237

Quoted 11 lines
drivers/cxl/core/region.c +11 -0
+		return -ENOENT;
+
+	/* reference to memdev held until target is unset or region goes away */
+
+	cxlmd = to_cxl_memdev(memdev_dev);
+	region->targets[n] = cxlmd;
+
+	device_unlock(&region->dev);
+
+	return len;
+}

J

#98 lore raw

RFC PATCH v2 20/28 cxl/region: Introduce a cxl_region driver

In reply to Ben Widawsky

The cxl_region driver is responsible for managing the HDM decoder programming in the CXL topology. Once a region is created it must be configured and bound to the driver in order to activate it.

The following is a sample of how such controls might work:

region=$(cat /sys/bus/cxl/devices/decoder0.0/create_region) echo $region > /sys/bus/cxl/devices/decoder0.0/create_region echo 2 > /sys/bus/cxl/devices/decoder0.0/region0.0:0/interleave echo $((256<<20)) > /sys/bus/cxl/devices/decoder0.0/region0.0:0/size echo mem0 > /sys/bus/cxl/devices/decoder0.0/region0.0:0/target0 echo region0.0:0 > /sys/bus/cxl/drivers/cxl_region/bind

In order to handle the eventual rise in failure modes of binding a region, a new trace event is created to help track these failures for debug and reconfiguration paths in userspace.

---
 .clang-format                                 |   1 +
 .../driver-api/cxl/memory-devices.rst         |   3 +
 drivers/cxl/Makefile                          |   2 +
 drivers/cxl/core/bus.c                        |  19 +-
 drivers/cxl/core/core.h                       |   1 +
 drivers/cxl/core/region.c                     |  25 +-
 drivers/cxl/cxl.h                             |   5 +
 drivers/cxl/region.c                          | 324 ++++++++++++++++++
 drivers/cxl/region.h                          |   4 +
 drivers/cxl/trace.h                           |  45 +++
 10 files changed, 425 insertions(+), 4 deletions(-)
 create mode 100644 drivers/cxl/region.c
 create mode 100644 drivers/cxl/trace.h
.clang-format +1 -0
index 15d4eaabc6b5..cb7c46371465 100644
@@ -169,6 +169,7 @@ ForEachMacros:
   - 'for_each_cpu_and'
   - 'for_each_cpu_not'
   - 'for_each_cpu_wrap'
+  - 'for_each_cxl_endpoint'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
   - 'for_each_dev_scope'
Documentation/driver-api/cxl/memory-devices.rst +3 -0
index 0ed00906acdd..fb5ed0c914b6 100644
@@ -67,6 +67,9 @@ CXL Core
 
 CXL Regions
 -----------
+.. kernel-doc:: drivers/cxl/region.c
+   :doc: cxl region
+
 .. kernel-doc:: drivers/cxl/region.h
    :identifiers:
 
drivers/cxl/Makefile +2 -0
index 77499a6b40f2..9936568711cd 100644
@@ -5,9 +5,11 @@ obj-$(CONFIG_CXL_PCI) += cxl_pci.o
 obj-$(CONFIG_CXL_MEM) += cxl_mem.o
 obj-$(CONFIG_CXL_ACPI) += cxl_acpi.o
 obj-$(CONFIG_CXL_PMEM) += cxl_pmem.o
+obj-$(CONFIG_CXL_MEM) += cxl_region.o
 
 cxl_mem-y := mem.o
 cxl_pci-y := pci.o
 cxl_acpi-y := acpi.o
 cxl_pmem-y := pmem.o
 cxl_port-y := port.o
+cxl_region-y := region.o
drivers/cxl/core/bus.c +18 -1
index 7a51a290ed25..3042e6e6f5b3 100644
@@ -7,6 +7,7 @@
 #include <linux/slab.h>
 #include <linux/idr.h>
 #include <cxlmem.h>
+#include <region.h>
 #include <cxl.h>
 #include <pci.h>
 #include "core.h"
@@ -32,6 +33,8 @@ static DECLARE_RWSEM(root_host_sem);
 
 static struct device *cxl_root_host;
 
+struct cxl_region *to_cxl_region(struct device *dev);
+
 int cxl_register_root(struct device *host)
 {
 	down_write(&root_host_sem);
@@ -962,6 +965,8 @@ static int cxl_device_id(struct device *dev)
 		return CXL_DEVICE_PORT;
 	if (dev->type == &cxl_memdev_type)
 		return CXL_DEVICE_MEMORY_EXPANDER;
+	if (dev->type == &cxl_region_type)
+		return CXL_DEVICE_REGION;
 	return 0;
 }
 
@@ -978,7 +983,19 @@ static int cxl_bus_match(struct device *dev, struct device_driver *drv)
 
 static int cxl_bus_probe(struct device *dev)
 {
-	return to_cxl_drv(dev->driver)->probe(dev);
+	int id = cxl_device_id(dev);
+
+	if (id == CXL_DEVICE_REGION) {
+		/* Regions cannot bind until parameters are set */
+		struct cxl_region *region = to_cxl_region(dev);
+
+		if (is_cxl_region_configured(region))
+			return to_cxl_drv(dev->driver)->probe(dev);
+	} else {
+		return to_cxl_drv(dev->driver)->probe(dev);
+	}
+
+	return -ENODEV;
 }
 
 static void cxl_bus_remove(struct device *dev)
drivers/cxl/core/core.h +1 -0
index dea246cb7c58..5bff806a8742 100644
@@ -7,6 +7,7 @@
 extern const struct device_type cxl_nvdimm_bridge_type;
 extern const struct device_type cxl_nvdimm_type;
 extern const struct device_type cxl_memdev_type;
+extern const struct device_type cxl_region_type;
 
 extern struct attribute_group cxl_base_attribute_group;
 
drivers/cxl/core/region.c +22 -3
index 3b0d74d4dd6c..f227d339f092 100644
@@ -11,6 +11,8 @@
 #include <cxlmem.h>
 #include <cxl.h>
 
+#include "core.h"
+
 /**
  * DOC: cxl core region
  *
@@ -25,10 +27,27 @@ static const struct attribute_group region_interleave_group;
 
 static bool is_region_active(struct cxl_region *region)
 {
-	/* TODO: Regions can't be activated yet. */
-	return false;
+	return region->active;
 }
 
+/*
+ * Most sanity checking is left up to region binding. This does the most basic
+ * check to determine whether or not the core should try probing the driver.
+ */
+bool is_cxl_region_configured(const struct cxl_region *region)
+{
+	/* zero sized regions aren't a thing. */
+	if (region->size <= 0)
+		return false;
+
+	/* all regions have at least 1 target */
+	if (!region->targets[0])
+		return false;
+
+	return true;
+}
+EXPORT_SYMBOL_GPL(is_cxl_region_configured);
+
 static void remove_target(struct cxl_region *region, int target)
 {
 	struct cxl_memdev *cxlmd;
@@ -310,7 +329,7 @@ static const struct attribute_group *region_groups[] = {
 
 static void cxl_region_release(struct device *dev);
 
-static const struct device_type cxl_region_type = {
+const struct device_type cxl_region_type = {
 	.name = "cxl_region",
 	.release = cxl_region_release,
 	.groups = region_groups
drivers/cxl/cxl.h +5 -0
index bcef2cb4e55b..430de5ab805a 100644
@@ -200,6 +200,10 @@ void cxl_unregister_root(void);
 #define CXL_DECODER_F_EN    BIT(5)
 #define CXL_DECODER_F_MASK  GENMASK(5, 0)
 
+#define cxl_is_pmem_t3(flags)                                                  \
+	(((flags) & (CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM)) ==             \
+	 (CXL_DECODER_F_TYPE3 | CXL_DECODER_F_PMEM))
+
 enum cxl_decoder_type {
        CXL_DECODER_ACCELERATOR = 2,
        CXL_DECODER_EXPANDER = 3,
@@ -362,6 +366,7 @@ void cxl_driver_unregister(struct cxl_driver *cxl_drv);
 #define CXL_DEVICE_NVDIMM		2
 #define CXL_DEVICE_PORT			3
 #define CXL_DEVICE_MEMORY_EXPANDER	4
+#define CXL_DEVICE_REGION		5
 
 #define MODULE_ALIAS_CXL(type) MODULE_ALIAS("cxl:t" __stringify(type) "*")
 #define CXL_MODALIAS_FMT "cxl:t%d"
drivers/cxl/region.c new file +324 -0

324 lines not on this page · raw message

drivers/cxl/region.h +4 -0
index d032df438832..5df417324cab 100644
@@ -12,6 +12,7 @@
  * @dev: This region's device.
  * @id: This regions id. Id is globally unique across all regions.
  * @list: Node in decoder's region list.
+ * @active: If the region has been activated.
  * @res: Address space consumed by this region.
  * @size: Size of the region determined from LSA or userspace.
  * @uuid: The UUID for this region.
@@ -23,6 +24,7 @@ struct cxl_region {
 	struct device dev;
 	int id;
 	struct list_head list;
+	bool active;
 
 	struct {
 		struct resource *res;
@@ -34,4 +36,6 @@ struct cxl_region {
 	};
 };
 
+bool is_cxl_region_configured(const struct cxl_region *region);
+
 #endif
drivers/cxl/trace.h new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.33.1
#99 lore raw

RFC PATCH v2 21/28 cxl/acpi: Handle address space allocation

In reply to Ben Widawsky

Regions are carved out of an addresses space which is claimed by top level decoders, and subsequently their children decoders. Regions are created with a size and therefore must fit, with proper alignment, in that address space. The support for doing this fitting is handled by the driver automatically.

As an example, a platform might configure a top level decoder to claim 1TB of address space @ 0x800000000 -> 0x10800000000; it would be possible to create M regions with appropriate alignment to occupy that address space. Each of those regions would have a host physical address somewhere in the range between 32G and 1.3TB, and the location will be determined by the logic added here.

The request_region() usage is not strictly mandatory at this point as the actual handling of the address space is done with genpools. It is highly likely however that the resource/region APIs will become useful in the not too distant future.

All decoders manage a host physical address space while active. Only the root decoder has constraints on location and size. As a result, it makes most sense for the root decoder to be responsible for managing the entire address space, and mid-level decoders and endpoints can ask the root decoder for suballocations.

---
 drivers/cxl/acpi.c | 30 ++++++++++++++++++++++++++++++
 drivers/cxl/cxl.h  |  2 ++
 2 files changed, 32 insertions(+)
drivers/cxl/acpi.c +30 -0
index 23afb3b8f5cc..65219b18f3bc 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright(c) 2021 Intel Corporation. All rights reserved. */
 #include <linux/platform_device.h>
+#include <linux/genalloc.h>
 #include <linux/module.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
@@ -75,6 +76,27 @@ static int cxl_acpi_cfmws_verify(struct device *dev,
 	return 0;
 }
 
+/*
+ * Every decoder while active has an address space that it is decoding. However,
+ * only the root level decoders have fixed host physical address space ranges.
+ */
+static int cxl_create_cfmws_address_space(struct cxl_decoder *cxld,
+					  struct acpi_cedt_cfmws *cfmws)
+{
+	const int order = ilog2(SZ_256M * cxld->interleave_ways);
+	struct device *dev = &cxld->dev;
+	struct gen_pool *pool;
+
+	pool = devm_gen_pool_create(dev, order, NUMA_NO_NODE, dev_name(dev));
+	if (IS_ERR(pool))
+		return PTR_ERR(pool);
+
+	cxld->address_space = pool;
+
+	return gen_pool_add(cxld->address_space, cfmws->base_hpa,
+			    cfmws->window_size, NUMA_NO_NODE);
+}
+
 static void cxl_add_cfmws_decoders(struct device *dev,
 				   struct cxl_port *root_port)
 {
@@ -133,6 +155,14 @@ static void cxl_add_cfmws_decoders(struct device *dev,
 		cxld->interleave_granularity =
 			CFMWS_INTERLEAVE_GRANULARITY(cfmws);
 
+		rc = cxl_create_cfmws_address_space(cxld, cfmws);
+		if (rc) {
+			dev_err(dev,
+				"Failed to create CFMWS address space for decoder\n");
+			put_device(&cxld->dev);
+			goto next;
+		}
+
 		rc = cxl_decoder_add(cxld, target_map);
 		if (rc)
 			put_device(&cxld->dev);
drivers/cxl/cxl.h +2 -0
index 430de5ab805a..509b20375e0e 100644
@@ -225,6 +225,7 @@ enum cxl_decoder_type {
  * @target_type: accelerator vs expander (type2 vs type3) selector
  * @flags: memory type capabilities and locking
  * @region_ida: allocator for region ids.
+ * @address_space: Used/free address space for regions.
  * @nr_targets: number of elements in @target
  * @target: active ordered target list in current decoder configuration
  */
@@ -237,6 +238,7 @@ struct cxl_decoder {
 	enum cxl_decoder_type target_type;
 	unsigned long flags;
 	struct ida region_ida;
+	struct gen_pool *address_space;
 	const int nr_targets;
 	struct cxl_dport *target[];
 };
Signature · 1 line
2.33.1
#100 lore raw

RFC PATCH v2 22/28 cxl/region: Address space allocation

In reply to Ben Widawsky

When a region is not assigned a host physical address, one is picked by the driver. As the address will determine which CFMWS contains the region, it's usually a better idea to let the driver make this determination.

---
 drivers/cxl/region.c | 35 +++++++++++++++++++++++++++++++++--
 drivers/cxl/trace.h  |  3 +++
 2 files changed, 36 insertions(+), 2 deletions(-)
drivers/cxl/region.c +33 -2
index da4f91b48743..d127c9c69eef 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright(c) 2021 Intel Corporation. All rights reserved. */
 #include <linux/platform_device.h>
+#include <linux/genalloc.h>
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/pci.h>
@@ -55,6 +56,17 @@ static struct cxl_port *get_root_decoder(const struct cxl_memdev *endpoint)
 	return NULL;
 }
 
+static void release_cxl_region(void *r)
+{
+	struct cxl_region *region = (struct cxl_region *)r;
+	struct cxl_decoder *cxld = cxld_from_region(region);
+	struct resource *parent_res = &cxld->res;
+	const struct resource *res = region->res;
+
+	gen_pool_free(cxld->address_space, res->start, resource_size(res));
+	__release_region(parent_res, res->start, resource_size(res));
+}
+
 /**
  * sanitize_region() - Check is region is reasonably configured
  * @region: The region to check
@@ -104,8 +116,27 @@ static int sanitize_region(const struct cxl_region *region)
  */
 static int allocate_address_space(struct cxl_region *region)
 {
-	/* TODO */
-	return 0;
+	struct cxl_decoder *cxld = cxld_from_region(region);
+	unsigned long start;
+
+	start = gen_pool_alloc(cxld->address_space, region->size);
+	if (!start) {
+		trace_allocation_failed(region,
+					"Couldn't allocate address space");
+		return -ENOMEM;
+	}
+	region->res =
+		__request_region(&cxld->res, start, region->size,
+				 dev_name(&region->dev), IORESOURCE_EXCLUSIVE);
+
+	if (IS_ERR(region->res)) {
+		trace_allocation_failed(region, "Couldn't obtain region");
+		gen_pool_free(cxld->address_space, start, region->size);
+		return PTR_ERR(region->res);
+	}
+
+	return devm_add_action_or_reset(&region->dev, release_cxl_region,
+					region);
 }
 
 /**
drivers/cxl/trace.h +3 -0
index 8f7f471e15b8..a53f00ba5d0e 100644
@@ -35,6 +35,9 @@ DEFINE_EVENT(cxl_region_template, region_activated,
 DEFINE_EVENT(cxl_region_template, sanitize_failed,
 	     TP_PROTO(const struct cxl_region *region, char *status),
 	     TP_ARGS(region, status));
+DEFINE_EVENT(cxl_region_template, allocation_failed,
+	     TP_PROTO(const struct cxl_region *region, char *status),
+	     TP_ARGS(region, status));
 
 #endif /* if !defined (__CXL_TRACE_H__) || defined(TRACE_HEADER_MULTI_READ) */
 
Signature · 1 line
2.33.1
#101 lore raw

RFC PATCH v2 23/28 cxl/region: Implement XHB verification

In reply to Ben Widawsky

Cross host bridge verification primarily determines if the requested interleave ordering can be achieved by the root decoder, which isn't as programmable as other decoders.

The algorithm implemented here is based on the CXL Type 3 Memory Device Software Guide, chapter 2.13.14

---
 .clang-format        |  1 +
 drivers/cxl/region.c | 81 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/trace.h  |  3 ++
 3 files changed, 84 insertions(+), 1 deletion(-)
.clang-format +1 -0
index cb7c46371465..55f628f21722 100644
@@ -169,6 +169,7 @@ ForEachMacros:
   - 'for_each_cpu_and'
   - 'for_each_cpu_not'
   - 'for_each_cpu_wrap'
+  - 'for_each_cxl_decoder_target'
   - 'for_each_cxl_endpoint'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
drivers/cxl/region.c +80 -1

99 lines not on this page · raw message

drivers/cxl/trace.h +3 -0
index a53f00ba5d0e..4de47d1111ac 100644
@@ -38,6 +38,9 @@ DEFINE_EVENT(cxl_region_template, sanitize_failed,
 DEFINE_EVENT(cxl_region_template, allocation_failed,
 	     TP_PROTO(const struct cxl_region *region, char *status),
 	     TP_ARGS(region, status));
+DEFINE_EVENT(cxl_region_template, xhb_valid,
+	     TP_PROTO(const struct cxl_region *region, char *status),
+	     TP_ARGS(region, status));
 
 #endif /* if !defined (__CXL_TRACE_H__) || defined(TRACE_HEADER_MULTI_READ) */
 
Signature · 1 line
2.33.1
#102 lore raw

Re: [RFC PATCH v2 23/28] cxl/region: Implement XHB verification

Quoted 31 lines · On Fri, 22 Oct 2021 11:37:04 -0700 Ben Widawsky <[email protected]> wrote:

Cross host bridge verification primarily determines if the requested interleave ordering can be achieved by the root decoder, which isn't as programmable as other decoders.

The algorithm implemented here is based on the CXL Type 3 Memory Device Software Guide, chapter 2.13.14

---
 .clang-format        |  1 +
 drivers/cxl/region.c | 81 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/trace.h  |  3 ++
 3 files changed, 84 insertions(+), 1 deletion(-)
.clang-format +1 -0
index cb7c46371465..55f628f21722 100644
@@ -169,6 +169,7 @@ ForEachMacros:
   - 'for_each_cpu_and'
   - 'for_each_cpu_not'
   - 'for_each_cpu_wrap'
+  - 'for_each_cxl_decoder_target'
   - 'for_each_cxl_endpoint'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
drivers/cxl/region.c +0 -0
index d127c9c69eef..53442de33d11 100644

drivers/cxl/region.c line 37

Quoted 8 lines
drivers/cxl/region.c +5 -0
@@ -30,6 +30,11 @@
 	for (idx = 0, ep = (region)->targets[idx]; idx < region_ways(region);  \
 	     idx++, ep = (region)->targets[idx])
 
+#define for_each_cxl_decoder_target(target, decoder, idx)                      \
+	for (idx = 0, target = (decoder)->target[idx];                         \
+	     idx < (decoder)->nr_targets;                                      \
+	     idx++, target = (decoder)->target[idx])
+

target used for too many things in this macro.

I'm messing around with this to poke some of the Qemu stuff and noticed this in passing...

Jonathan

#103 lore raw

Re: [RFC PATCH v2 23/28] cxl/region: Implement XHB verification

Quoted 44 lines · Jonathan Cameron
Quoted 31 lines · On Fri, 22 Oct 2021 11:37:04 -0700 Ben Widawsky <[email protected]> wrote:

Cross host bridge verification primarily determines if the requested interleave ordering can be achieved by the root decoder, which isn't as programmable as other decoders.

The algorithm implemented here is based on the CXL Type 3 Memory Device Software Guide, chapter 2.13.14

---
 .clang-format        |  1 +
 drivers/cxl/region.c | 81 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/trace.h  |  3 ++
 3 files changed, 84 insertions(+), 1 deletion(-)
.clang-format +1 -0
index cb7c46371465..55f628f21722 100644
@@ -169,6 +169,7 @@ ForEachMacros:
   - 'for_each_cpu_and'
   - 'for_each_cpu_not'
   - 'for_each_cpu_wrap'
+  - 'for_each_cxl_decoder_target'
   - 'for_each_cxl_endpoint'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
drivers/cxl/region.c +0 -0
index d127c9c69eef..53442de33d11 100644

drivers/cxl/region.c line 37

Quoted 8 lines
drivers/cxl/region.c +5 -0
@@ -30,6 +30,11 @@
 	for (idx = 0, ep = (region)->targets[idx]; idx < region_ways(region);  \
 	     idx++, ep = (region)->targets[idx])
 
+#define for_each_cxl_decoder_target(target, decoder, idx)                      \
+	for (idx = 0, target = (decoder)->target[idx];                         \
+	     idx < (decoder)->nr_targets;                                      \
+	     idx++, target = (decoder)->target[idx])
+

target used for too many things in this macro.

Quoted 4 lines

I'm messing around with this to poke some of the Qemu stuff and noticed this in passing...

Jonathan

Thanks.

BTW, I have some rather large changes in flight. Might be good to check this branch (I'm in force push mode):
https://gitlab.com/bwidawsk/linux/-/commits/cxl_region

Also, I have a minor QEMU change (HACK) to support multiple root ports.
https://gitlab.com/bwidawsk/qemu/-/commit/7c76849f9a4d2bc5fc9c355ed06ea926fc7ab494

#104 lore raw

Re: [RFC PATCH v2 23/28] cxl/region: Implement XHB verification

Quoted 50 lines · On Thu, 6 Jan 2022 08:58:15 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 44 lines · Jonathan Cameron
Quoted 31 lines · On Fri, 22 Oct 2021 11:37:04 -0700 Ben Widawsky <[email protected]> wrote:

Cross host bridge verification primarily determines if the requested interleave ordering can be achieved by the root decoder, which isn't as programmable as other decoders.

The algorithm implemented here is based on the CXL Type 3 Memory Device Software Guide, chapter 2.13.14

---
 .clang-format        |  1 +
 drivers/cxl/region.c | 81 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/trace.h  |  3 ++
 3 files changed, 84 insertions(+), 1 deletion(-)
.clang-format +1 -0
index cb7c46371465..55f628f21722 100644
@@ -169,6 +169,7 @@ ForEachMacros:
   - 'for_each_cpu_and'
   - 'for_each_cpu_not'
   - 'for_each_cpu_wrap'
+  - 'for_each_cxl_decoder_target'
   - 'for_each_cxl_endpoint'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
drivers/cxl/region.c +0 -0
index d127c9c69eef..53442de33d11 100644

drivers/cxl/region.c line 37

Quoted 8 lines
drivers/cxl/region.c +5 -0
@@ -30,6 +30,11 @@
 	for (idx = 0, ep = (region)->targets[idx]; idx < region_ways(region);  \
 	     idx++, ep = (region)->targets[idx])
 
+#define for_each_cxl_decoder_target(target, decoder, idx)                      \
+	for (idx = 0, target = (decoder)->target[idx];                         \
+	     idx < (decoder)->nr_targets;                                      \
+	     idx++, target = (decoder)->target[idx])
+  

target used for too many things in this macro.

Quoted 4 lines

I'm messing around with this to poke some of the Qemu stuff and noticed this in passing...

Jonathan

Quoted 8 lines

Thanks.

BTW, I have some rather large changes in flight. Might be good to check this branch (I'm in force push mode):
https://gitlab.com/bwidawsk/linux/-/commits/cxl_region

Also, I have a minor QEMU change (HACK) to support multiple root ports.
https://gitlab.com/bwidawsk/qemu/-/commit/7c76849f9a4d2bc5fc9c355ed06ea926fc7ab494

Thanks. Will take a look at both.

Mostly I'm interested in the QEMU side of things and trying to get a cleaner command line working but good to have a way to poke it an check the CFMWS is correct etc.

Jonathan

#105 lore raw

Re: [RFC PATCH v2 23/28] cxl/region: Implement XHB verification

Quoted 53 lines · On Thu, 6 Jan 2022 17:33:46 +0000 Jonathan Cameron…
Quoted 50 lines · On Thu, 6 Jan 2022 08:58:15 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 48 lines · Jonathan Cameron
Quoted 31 lines · On Fri, 22 Oct 2021 11:37:04 -0700 Ben Widawsky <[email protected]> wrote:

Cross host bridge verification primarily determines if the requested interleave ordering can be achieved by the root decoder, which isn't as programmable as other decoders.

The algorithm implemented here is based on the CXL Type 3 Memory Device Software Guide, chapter 2.13.14

---
 .clang-format        |  1 +
 drivers/cxl/region.c | 81 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/trace.h  |  3 ++
 3 files changed, 84 insertions(+), 1 deletion(-)
.clang-format +1 -0
index cb7c46371465..55f628f21722 100644
@@ -169,6 +169,7 @@ ForEachMacros:
   - 'for_each_cpu_and'
   - 'for_each_cpu_not'
   - 'for_each_cpu_wrap'
+  - 'for_each_cxl_decoder_target'
   - 'for_each_cxl_endpoint'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
drivers/cxl/region.c +0 -0
index d127c9c69eef..53442de33d11 100644

drivers/cxl/region.c line 37

Quoted 8 lines
drivers/cxl/region.c +5 -0
@@ -30,6 +30,11 @@
 	for (idx = 0, ep = (region)->targets[idx]; idx < region_ways(region);  \
 	     idx++, ep = (region)->targets[idx])
 
+#define for_each_cxl_decoder_target(target, decoder, idx)                      \
+	for (idx = 0, target = (decoder)->target[idx];                         \
+	     idx < (decoder)->nr_targets;                                      \
+	     idx++, target = (decoder)->target[idx])
+    

target used for too many things in this macro.

I'm messing around with this to poke some of the Qemu stuff and noticed this in passing...

Jonathan

Quoted 8 lines
Quoted 8 lines

Thanks.

BTW, I have some rather large changes in flight. Might be good to check this branch (I'm in force push mode):
https://gitlab.com/bwidawsk/linux/-/commits/cxl_region

Also, I have a minor QEMU change (HACK) to support multiple root ports.
https://gitlab.com/bwidawsk/qemu/-/commit/7c76849f9a4d2bc5fc9c355ed06ea926fc7ab494

If we were feeling lazy that could (I think) just be set to the maximum allowed and be 'correct' in all cases.

Quoted 5 lines

Thanks. Will take a look at both.

Mostly I'm interested in the QEMU side of things and trying to get a cleaner command line working but good to have a way to poke it an check the CFMWS is correct etc.

FYI. I'll leave feedback for where I'm hitting bugs on your gitlab branches. My test setup that I'm trying to build regions on is
2 host bridge, 2 ports on each, 1 device directly connected to both. The qemu code will unfortunately take a bit of extracting from company internals so I want to get a bit further with it before going the effort of doing that and I have a few other things on my todo list.

Jonathan

Quoted 4 lines

Jonathan

#106 lore raw

Re: [RFC PATCH v2 23/28] cxl/region: Implement XHB verification

Quoted 62 lines · Jonathan Cameron
Quoted 59 lines · On Thu, 6 Jan 2022 17:33:46 +0000 Jonathan Cameron…
Quoted 56 lines · On Thu, 6 Jan 2022 08:58:15 -0800 Ben Widawsky <[email protected]> wrote:
Quoted 44 lines · Jonathan Cameron
Quoted 31 lines · On Fri, 22 Oct 2021 11:37:04 -0700 Ben Widawsky <[email protected]> wrote:

Cross host bridge verification primarily determines if the requested interleave ordering can be achieved by the root decoder, which isn't as programmable as other decoders.

The algorithm implemented here is based on the CXL Type 3 Memory Device Software Guide, chapter 2.13.14

---
 .clang-format        |  1 +
 drivers/cxl/region.c | 81 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/trace.h  |  3 ++
 3 files changed, 84 insertions(+), 1 deletion(-)
.clang-format +1 -0
index cb7c46371465..55f628f21722 100644
@@ -169,6 +169,7 @@ ForEachMacros:
   - 'for_each_cpu_and'
   - 'for_each_cpu_not'
   - 'for_each_cpu_wrap'
+  - 'for_each_cxl_decoder_target'
   - 'for_each_cxl_endpoint'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
drivers/cxl/region.c +0 -0
index d127c9c69eef..53442de33d11 100644

drivers/cxl/region.c line 37

Quoted 8 lines
drivers/cxl/region.c +5 -0
@@ -30,6 +30,11 @@
 	for (idx = 0, ep = (region)->targets[idx]; idx < region_ways(region);  \
 	     idx++, ep = (region)->targets[idx])
 
+#define for_each_cxl_decoder_target(target, decoder, idx)                      \
+	for (idx = 0, target = (decoder)->target[idx];                         \
+	     idx < (decoder)->nr_targets;                                      \
+	     idx++, target = (decoder)->target[idx])
+    

target used for too many things in this macro.

Quoted 4 lines

I'm messing around with this to poke some of the Qemu stuff and noticed this in passing...

Jonathan

Thanks.

BTW, I have some rather large changes in flight. Might be good to check this branch (I'm in force push mode):
https://gitlab.com/bwidawsk/linux/-/commits/cxl_region

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Also, I have a minor QEMU change (HACK) to support multiple root ports.
https://gitlab.com/bwidawsk/qemu/-/commit/7c76849f9a4d2bc5fc9c355ed06ea926fc7ab494

If we were feeling lazy that could (I think) just be set to the maximum allowed and be 'correct' in all cases.

Yeah. For validation standpoint, having it be a prop is nice, but I think default as max rather than 1 is smart.

Quoted 6 lines
Quoted 5 lines

Thanks. Will take a look at both.

Mostly I'm interested in the QEMU side of things and trying to get a cleaner command line working but good to have a way to poke it an check the CFMWS is correct etc.

Quoted 6 lines

FYI. I'll leave feedback for where I'm hitting bugs on your gitlab branches. My test setup that I'm trying to build regions on is
2 host bridge, 2 ports on each, 1 device directly connected to both. The qemu code will unfortunately take a bit of extracting from company internals so I want to get a bit further with it before going the effort of doing that and I have a few other things on my todo list.

Okay, thanks.

Quoted 8 lines

Jonathan

Quoted 4 lines

Jonathan

#107 lore raw

RFC PATCH v2 24/28 cxl/region: HB port config verification

In reply to Ben Widawsky

Host bridge root port verification determines if the device ordering in an interleave set can be programmed through the host bridges and switches.

The algorithm implemented here is based on the CXL Type 3 Memory Device Software Guide, chapter 2.13.15

---
 .clang-format        |  1 +
 drivers/cxl/cxl.h    |  3 ++
 drivers/cxl/region.c | 90 +++++++++++++++++++++++++++++++++++++++++++-
 drivers/cxl/trace.h  |  3 ++
 4 files changed, 96 insertions(+), 1 deletion(-)
.clang-format +1 -0
index 55f628f21722..96c282b63e7b 100644
@@ -171,6 +171,7 @@ ForEachMacros:
   - 'for_each_cpu_wrap'
   - 'for_each_cxl_decoder_target'
   - 'for_each_cxl_endpoint'
+  - 'for_each_cxl_endpoint_hb'
   - 'for_each_dapm_widgets'
   - 'for_each_dev_addr'
   - 'for_each_dev_scope'
drivers/cxl/cxl.h +3 -0
index 509b20375e0e..63554c9cebf0 100644
@@ -303,6 +303,7 @@ struct cxl_port {
  * @port: reference to cxl_port that contains this downstream port
  * @list: node for a cxl_port's list of cxl_dport instances
  * @root_port_link: node for global list of root ports
+ * @verify_link: node used for hb root port verification
  */
 struct cxl_dport {
 	struct device *dport;
@@ -311,6 +312,8 @@ struct cxl_dport {
 	struct cxl_port *port;
 	struct list_head list;
 	struct list_head root_port_link;
+
+	struct list_head verify_link;
 };
 
 bool is_cxl_region(struct device *dev);
drivers/cxl/region.c +89 -1

114 lines not on this page · raw message

drivers/cxl/trace.h +3 -0
index 4de47d1111ac..57fe9342817c 100644
@@ -41,6 +41,9 @@ DEFINE_EVENT(cxl_region_template, allocation_failed,
 DEFINE_EVENT(cxl_region_template, xhb_valid,
 	     TP_PROTO(const struct cxl_region *region, char *status),
 	     TP_ARGS(region, status));
+DEFINE_EVENT(cxl_region_template, hb_rp_valid,
+	     TP_PROTO(const struct cxl_region *region, char *status),
+	     TP_ARGS(region, status));
 
 #endif /* if !defined (__CXL_TRACE_H__) || defined(TRACE_HEADER_MULTI_READ) */
 
Signature · 1 line
2.33.1
#108 lore raw

RFC PATCH v2 25/28 cxl/region: Record host bridge target list

In reply to Ben Widawsky

Part of host bridge verification in the CXL Type 3 Memory Device Software Guide calculates the host bridge interleave target list (6th step in the flow chart). With host bridge verification already done, it is trivial to store away the configuration information.

TODO: Needs support for switches (7th step in the flow chart).

---
 drivers/cxl/region.c | 41 ++++++++++++++++++++++++++++++-----------
 drivers/cxl/region.h | 12 ++++++++++++
 2 files changed, 42 insertions(+), 11 deletions(-)
drivers/cxl/region.c +30 -11
index ecf453fd3257..3276c7243c2a 100644
@@ -294,14 +294,17 @@ static int get_num_root_ports(const struct cxl_region *region)
  * region_hb_rp_config_valid() - determine root port ordering is correct
  * @cfmws: CFMWS decoder for this @region
  * @region: Region to validate
+ * @p: HDM decoder programming state. Populated if non-NULL.
  *
  * The algorithm is outlined in 2.13.15 "Verify HB root port configuration
  * sequence" of the CXL Memory Device SW Guide (Rev1p0).
  *
- * Returns true if the configuration is valid.
+ * Returns true if the configuration is valid, the configuration state is
+ * updated for later programming.
  */
 static bool region_hb_rp_config_valid(const struct cxl_region *region,
-				      const struct cxl_decoder *cfmws)
+				      const struct cxl_decoder *cfmws,
+				      struct decoder_programming *p)
 {
 	const int num_root_ports = get_num_root_ports(region);
 	struct cxl_port *hbs[CXL_DECODER_MAX_INTERLEAVE];
@@ -309,18 +312,29 @@ static bool region_hb_rp_config_valid(const struct cxl_region *region,
 
 	hb_count = get_unique_hostbridges(region, hbs);
 
+	if (p)
+		p->hb_count = hb_count;
+
 	/*
 	 * Are all devices in this region on the same CXL Host Bridge
 	 * Root Port?
 	 */
-	if (num_root_ports == 1)
+	if (num_root_ports == 1) {
+		if (p) {
+			p->hbs[0].rp_target_list[0] = region->targets[0]->root_port;
+			p->hbs[0].rp_count = 1;
+		}
 		return true;
+	}
 
 	for (i = 0; i < hb_count; i++) {
+		struct cxl_dport *rp, **targets;
 		struct cxl_port *hb = hbs[i];
-		struct cxl_dport *rp;
 		int position_mask;
-		int idx;
+		int idx, *rp_count;
+
+		targets = &p->hbs[i].rp_target_list[0];
+		rp_count = &p->hbs[i].rp_count;
 
 		/*
 		 * Calculate the position mask: NumRootPorts = 2^PositionMask
@@ -343,9 +357,12 @@ static bool region_hb_rp_config_valid(const struct cxl_region *region,
 				if (ep->root_port != rp)
 					continue;
 
-				if (port_grouping == -1) {
+				if (port_grouping == -1)
 					port_grouping = idx & position_mask;
-					continue;
+
+				if (p) {
+					(*rp_count)++;
+					targets[port_grouping] = ep->root_port;
 				}
 
 				/*
@@ -379,7 +396,8 @@ static bool cfmws_contains(const struct cxl_region *region,
 }
 
 static bool cfmws_valid(const struct cxl_region *region,
-			const struct cxl_decoder *cfmws)
+			const struct cxl_decoder *cfmws,
+			struct decoder_programming *p)
 {
 	const struct cxl_memdev *endpoint = region->targets[0];
 
@@ -392,7 +410,7 @@ static bool cfmws_valid(const struct cxl_region *region,
 	if (!region_xhb_config_valid(region, cfmws))
 		return false;
 
-	if (!region_hb_rp_config_valid(region, cfmws))
+	if (!region_hb_rp_config_valid(region, cfmws, p))
 		return false;
 
 	if (!cfmws_contains(region, cfmws))
@@ -409,7 +427,7 @@ static int cfmws_match(struct device *dev, void *data)
 	if (!is_root_decoder(dev))
 		return 0;
 
-	return !!cfmws_valid(region, to_cxl_decoder(dev));
+	return !!cfmws_valid(region, to_cxl_decoder(dev), NULL);
 }
 
 /*
@@ -484,7 +502,8 @@ static int cxl_region_probe(struct device *dev)
 		return -ENXIO;
 
 	cfmws = cxld_from_region(region);
-	if (!cfmws_valid(region, cfmws)) {
+	if (!cfmws_valid(region, cfmws,
+			 (struct decoder_programming *)&region->state)) {
 		dev_err(dev, "Picked invalid cfmws\n");
 		return -ENXIO;
 	}
drivers/cxl/region.h +12 -0
index 5df417324cab..51f442636364 100644
@@ -19,6 +19,10 @@
  * @eniw: Number of interleave ways this region is configured for.
  * @ig: Interleave granularity of region
  * @targets: The memory devices comprising the region.
+ * @state: Configuration state for host bridges, switches, and endpoints.
+ * @state.hbs: Host bridge state. One per hostbridge in the interleave set.
+ * @state.hbs.rp_count: Count of root ports for this region
+ * @state.hbs.rp_target_list: Ordered list of downstream root ports.
  */
 struct cxl_region {
 	struct device dev;
@@ -34,6 +38,14 @@ struct cxl_region {
 		int ig;
 		struct cxl_memdev *targets[CXL_DECODER_MAX_INTERLEAVE];
 	};
+
+	struct decoder_programming {
+		int hb_count;
+		struct {
+			int rp_count;
+			struct cxl_dport *rp_target_list[CXL_DECODER_MAX_INTERLEAVE];
+		} hbs[CXL_DECODER_MAX_INTERLEAVE];
+	} state;
 };
 
 bool is_cxl_region_configured(const struct cxl_region *region);
Signature · 1 line
2.33.1
#109 lore raw

RFC PATCH v2 26/28 cxl/mem: Store the endpoint's uport

In reply to Ben Widawsky

Keeping track of an endpoint's port is important when attempting to assemble all relevant components in the CXL.mem route from root to endpoint. Without this change, the endpoint's port can be obtained through walks of the hierarchy.

---
 drivers/cxl/cxlmem.h | 1 +
 drivers/cxl/mem.c    | 5 ++---
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/cxl/cxlmem.h +1 -0
index cc5844150ce0..02efb270acac 100644
@@ -45,6 +45,7 @@ struct cxl_memdev {
 	int id;
 	resource_size_t creg_base;
 	struct cxl_dport *root_port;
+	struct cxl_port *uport;
 };
 
 static inline struct cxl_memdev *to_cxl_memdev(struct device *dev)
drivers/cxl/mem.c +2 -3
index 0b0272576e55..d348056f320f 100644
@@ -61,8 +61,7 @@ static void remove_endpoint(void *_cxlmd)
 	if (cxlmd->root_port)
 		sysfs_remove_link(&cxlmd->dev.kobj, "root_port");
 
-	endpoint = dev_get_drvdata(&cxlmd->dev);
-
+	endpoint = cxlmd->uport;
 	devm_cxl_remove_port(endpoint);
 }
 
@@ -83,7 +82,7 @@ static int create_endpoint(struct device *dev, struct cxl_port *parent,
 		device_del(&endpoint->dev);
 		return rc;
 	}
-	dev_set_drvdata(dev, endpoint);
+	cxlmd->uport = endpoint;
 	dev_dbg(dev, "add: %s\n", dev_name(&endpoint->dev));
 
 	return devm_add_action_or_reset(dev, remove_endpoint, cxlmd);
Signature · 1 line
2.33.1
#110 lore raw

RFC PATCH v2 27/28 cxl/region: Gather HDM decoder resources

In reply to Ben Widawsky

Prepare for HDM decoder programming by iterating through all components and obtaining a cxl_decoder for each.

Programming a CXL region to accept memory transactions over a set of devices requires programming HDM decoders for every component that is part of the hierarchy enabling the region (host bridges, switches, and endpoints). For this to be possible, each of these components must have an available HDM decoder, which is a limited hardware resource.

---
 drivers/cxl/core/bus.c |  8 ++++++
 drivers/cxl/cxl.h      |  3 ++
 drivers/cxl/port.c     | 53 ++++++++++++++++++++++++++++++++++
 drivers/cxl/region.c   | 65 +++++++++++++++++++++++++++++++++++++++---
 drivers/cxl/region.h   |  4 +++
 drivers/cxl/trace.h    | 21 ++++++++++++++
 6 files changed, 150 insertions(+), 4 deletions(-)
drivers/cxl/core/bus.c +8 -0
index 3042e6e6f5b3..a75fb2c7e094 100644
@@ -327,6 +327,14 @@ static bool is_endpoint_decoder(struct device *dev)
 	return dev->type == &cxl_decoder_endpoint_type;
 }
 
+bool is_cxl_decoder(struct device *dev)
+{
+	return dev->type == &cxl_decoder_switch_type ||
+	       dev->type == &cxl_decoder_endpoint_type ||
+	       dev->type == &cxl_decoder_root_type;
+}
+EXPORT_SYMBOL_GPL(is_cxl_decoder);
+
 bool is_root_decoder(struct device *dev)
 {
 	return dev->type == &cxl_decoder_root_type;
drivers/cxl/cxl.h +3 -0
index 63554c9cebf0..79d22992fddf 100644
@@ -335,11 +335,14 @@ int cxl_add_dport(struct cxl_port *port, struct device *dport, int port_id,
 struct cxl_dport *cxl_get_root_dport(struct device *dev);
 struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port,
 					struct device *dev);
+struct cxl_decoder *cxl_pop_decoder(struct cxl_port *port);
+void cxl_push_decoder(struct cxl_decoder *cxld);
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
 struct cxl_decoder *cxl_decoder_alloc(struct cxl_port *port,
 				      unsigned int nr_targets);
+bool is_cxl_decoder(struct device *dev);
 int cxl_decoder_add_locked(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_add(struct cxl_decoder *cxld, int *target_map);
 int cxl_decoder_autoremove(struct device *host, struct cxl_decoder *cxld);
drivers/cxl/port.c +53 -0

59 lines not on this page · raw message

drivers/cxl/region.c +61 -4

87 lines not on this page · raw message

drivers/cxl/region.h +4 -0
index 51f442636364..4c0b94c3c001 100644
@@ -23,6 +23,8 @@
  * @state.hbs: Host bridge state. One per hostbridge in the interleave set.
  * @state.hbs.rp_count: Count of root ports for this region
  * @state.hbs.rp_target_list: Ordered list of downstream root ports.
+ * @state.hbs.cxld: an available decoder to set up the programming.
+ * @state.ep_cxld: available decoders for endpoint programming.
  */
 struct cxl_region {
 	struct device dev;
@@ -44,7 +46,9 @@ struct cxl_region {
 		struct {
 			int rp_count;
 			struct cxl_dport *rp_target_list[CXL_DECODER_MAX_INTERLEAVE];
+			struct cxl_decoder *cxld;
 		} hbs[CXL_DECODER_MAX_INTERLEAVE];
+		struct cxl_decoder *ep_cxld[CXL_DECODER_MAX_INTERLEAVE];
 	} state;
 };
 
drivers/cxl/trace.h +21 -0
index 57fe9342817c..516a7aed8a27 100644
@@ -45,6 +45,27 @@ DEFINE_EVENT(cxl_region_template, hb_rp_valid,
 	     TP_PROTO(const struct cxl_region *region, char *status),
 	     TP_ARGS(region, status));
 
+TRACE_EVENT(cxl_region_decoder,
+	TP_PROTO(const struct cxl_region *region, struct cxl_port *port),
+
+	TP_ARGS(region, port),
+
+	TP_STRUCT__entry(
+		__field(const struct cxl_region *, region)
+		__field(struct cxl_port *, port)
+		__string(rdev_name, dev_name(&region->dev))
+		__string(pdev_name, dev_name(&port->dev))
+	),
+
+	TP_fast_assign(
+		__assign_str(rdev_name, dev_name(&region->dev));
+		__assign_str(pdev_name, dev_name(&port->dev));
+	),
+
+	TP_printk("%s: HDM decoder error for %s", __get_str(rdev_name), __get_str(pdev_name))
+);
+
+
 #endif /* if !defined (__CXL_TRACE_H__) || defined(TRACE_HEADER_MULTI_READ) */
 
 /* This part must be outside protection */
Signature · 1 line
2.33.1
#111 lore raw

RFC PATCH v2 28/28 cxl: Program decoders for regions

In reply to Ben Widawsky

Do the HDM decoder programming for all endpoints and host bridges in a region. Switches are currently unimplemented.

---
 drivers/cxl/cxl.h    |   3 +
 drivers/cxl/port.c   | 196 +++++++++++++++++++++++++++++++++++++++++++
 drivers/cxl/region.c |  33 +++++++-
 3 files changed, 231 insertions(+), 1 deletion(-)
drivers/cxl/cxl.h +3 -0
index 79d22992fddf..f2ee26ac2398 100644
@@ -52,6 +52,7 @@
 #define   CXL_HDM_DECODER0_CTRL_IW_MASK GENMASK(7, 4)
 #define   CXL_HDM_DECODER0_CTRL_COMMIT BIT(9)
 #define   CXL_HDM_DECODER0_CTRL_COMMITTED BIT(10)
+#define   CXL_HDM_DECODER0_CTRL_COMMIT_ERROR BIT(11)
 #define   CXL_HDM_DECODER0_CTRL_TYPE BIT(12)
 #define CXL_HDM_DECODER0_TL_LOW(i) (0x20 * (i) + 0x24)
 #define CXL_HDM_DECODER0_TL_HIGH(i) (0x20 * (i) + 0x28)
@@ -337,6 +338,8 @@ struct cxl_dport *cxl_find_dport_by_dev(struct cxl_port *port,
 					struct device *dev);
 struct cxl_decoder *cxl_pop_decoder(struct cxl_port *port);
 void cxl_push_decoder(struct cxl_decoder *cxld);
+int cxl_commit_decoder(struct cxl_decoder *cxld);
+void cxl_disable_decoder(struct cxl_decoder *cxld);
 
 struct cxl_decoder *to_cxl_decoder(struct device *dev);
 bool is_root_decoder(struct device *dev);
drivers/cxl/port.c +196 -0

202 lines not on this page · raw message

drivers/cxl/region.c +32 -1
index f4d190ede3ee..05e7ca3d0900 100644
@@ -526,7 +526,38 @@ static int gather_hdm_decoders(const struct cxl_region *region, struct decoder_p
 
 static int bind_region(const struct cxl_region *region)
 {
-	/* TODO: */
+	const struct decoder_programming *p = &region->state;
+	int i, rc;
+
+	for (i = 0; i < p->hb_count; i++) {
+		struct cxl_decoder *cxld = p->hbs[i].cxld;
+		int j;
+
+		cxld->res = (struct resource)DEFINE_RES_MEM(region->res->start,
+							    region->size);
+		cxld->interleave_granularity = region->ig;
+		cxld->interleave_ways = p->hbs[i].rp_count;
+		for (j = 0; j < p->hbs[i].rp_count; j++)
+			cxld->target[j] = p->hbs[i].rp_target_list[j];
+
+		rc = cxl_commit_decoder(cxld);
+		if (rc)
+			return rc;
+	}
+
+	for (i = 0; i < region_ways(region); i++) {
+		struct cxl_decoder *cxld = p->ep_cxld[i];
+
+		cxld->res = (struct resource)DEFINE_RES_MEM(region->res->start,
+							    region->size);
+		cxld->interleave_granularity = region->ig;
+		cxld->interleave_ways = region_ways(region);
+
+		rc = cxl_commit_decoder(cxld);
+		if (rc)
+			return rc;
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.33.1