Skip to contentSkip to conversation

add support for CDX bus

PATCH Superseded Nipun Gupta 3 participants 12 messages Mar 7, 2023 – Mar 10, 2023

Subsystems (4)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Nipun Gupta

author maintainer
#0 lore raw

v9 cover add support for CDX bus

Introduce AMD CDX bus, which provides a mechanism to discover/rescan CDX devices. The CDX devices are memory mapped on system bus for embedded CPUs.

CDX controller interacts with the firmware to query different CDX devices present in the Fabric and expose them to the Linux host on CDX bus.

This patch series:
- Introduces the CDX bus and CDX devices.
- Device tree binding for CDX controller
- Support for CDX bus in arm-smmu-v3 driver
- Add MCDI (Management CPU Driver Interface) as a protocol for communication with RPU Firmware
- Support RPMSg channel for Firmware communication

v8 patch series is at:
https://lore.kernel.org/lkml/[email protected]/T/

Changes v8 -> v9
- Update the name of patch (PATCH 2/7 - iommu: Support ops registration for CDX bus)
- update a log print and return variable names

Changes v7 -> v8
- Add ranges property in device tree bindings

Changes v6 -> v7
- Fix compilation for x86 environment in case CDX bus is enabled
- Fixed crash in reset if device is not bound to any device driver
- Update sysfs with typo and other minor changes
- Removed header file issue reported by make versioncheck

Changes v5 -> v6
- updated compatible name in the CDX dt-binding
- updated reset CDX device and removed redundant function
- moved from drivers/bus/cdx to drivers/cdx
- used xarray instead of controller list and ID
- updated sysfs documentation with more details

Changes RFC v4 -> v5
- Fixed device tree documentation
- Add MCDI as a protocol and RPMsg as transport for communication with RPU Firmware instead of using MCDI stubs.
- MSI patches for CDX are not added in this series as it's support is being revisited as per patch series:
https://lore.kernel.org/all/[email protected]/
It will be added as separate patches.

Changes RFC v3 -> RFC v4:
- Separate CDX bus and CDX controller driver (Greg K-H)
- Added MSI interfacing to Firmware for writing MSI message to firmware so it can be provided to the device.
- Fix MSI review comments - multiple cleanups (Mark Zynger)
- Fix the device tree yaml compilation (Rob Herring, Krzysztof)
- removed vfio-cdx from this series. It will be added after bus support is complete (Jason)

Changes RFC v2 -> RFC v3:
- Move CDX bus as a new bus type in kernel rather than using the platform devices (Greg K-H, Marc Zynger)
- Correspondingly update ARM SMMU v3
- Add support for vfio-cdx driver
- Updated device tree yaml with correct binding information (Krzysztof Kozlowski)
- remove 'compatible' sysfs platform patch which was required for CDX devices exposed as platform devices

Changes RFC v1 -> RFC v2:
- introduce CDX bus infrastructure
- fixed code for making compatible visible for devices having the 'compatible' property only (Greg K-H)
- moved CDX-MSI domain as part of CDX bus infrastructure. previously it was part of irqchip (Marc Zynger).
- fixed few prints (Greg K-H)
- support rescan and reset of CDX bus
- add VFIO reset module for CDX bus based devices

Abhijit Gangurde (1):
cdx: add rpmsg communication channel for CDX

Nipun Gupta (6):
cdx: add the cdx bus driver
iommu: Support ops registration for CDX bus
dt-bindings: bus: add CDX bus controller for versal net
cdx: add MCDI protocol interface for firmware interaction
cdx: add cdx controller
cdx: add device attributes

 Documentation/ABI/testing/sysfs-bus-cdx       |  56 ++
 .../bindings/bus/xlnx,versal-net-cdx.yaml     |  82 ++
 MAINTAINERS                                   |   8 +
 drivers/Kconfig                               |   2 +
 drivers/Makefile                              |   1 +
 drivers/cdx/Kconfig                           |  19 +
 drivers/cdx/Makefile                          |   8 +
 drivers/cdx/cdx.c                             | 552 +++++++++++
 drivers/cdx/cdx.h                             |  62 ++
 drivers/cdx/controller/Kconfig                |  30 +
 drivers/cdx/controller/Makefile               |   9 +
 drivers/cdx/controller/bitfield.h             |  90 ++
 drivers/cdx/controller/cdx_controller.c       | 231 +++++
 drivers/cdx/controller/cdx_controller.h       |  30 +
 drivers/cdx/controller/cdx_rpmsg.c            | 202 ++++
 drivers/cdx/controller/mc_cdx_pcol.h          | 590 ++++++++++++
 drivers/cdx/controller/mcdi.c                 | 903 ++++++++++++++++++
 drivers/cdx/controller/mcdi.h                 | 248 +++++
 drivers/cdx/controller/mcdi_functions.c       | 139 +++
 drivers/cdx/controller/mcdi_functions.h       |  61 ++
 drivers/iommu/iommu.c                         |   4 +
 include/linux/cdx/cdx_bus.h                   | 174 ++++
 include/linux/mod_devicetable.h               |  15 +
 scripts/mod/devicetable-offsets.c             |   4 +
 scripts/mod/file2alias.c                      |  12 +
 25 files changed, 3532 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-cdx
 create mode 100644 Documentation/devicetree/bindings/bus/xlnx,versal-net-cdx.yaml
 create mode 100644 drivers/cdx/Kconfig
 create mode 100644 drivers/cdx/Makefile
 create mode 100644 drivers/cdx/cdx.c
 create mode 100644 drivers/cdx/cdx.h
 create mode 100644 drivers/cdx/controller/Kconfig
 create mode 100644 drivers/cdx/controller/Makefile
 create mode 100644 drivers/cdx/controller/bitfield.h
 create mode 100644 drivers/cdx/controller/cdx_controller.c
 create mode 100644 drivers/cdx/controller/cdx_controller.h
 create mode 100644 drivers/cdx/controller/cdx_rpmsg.c
 create mode 100644 drivers/cdx/controller/mc_cdx_pcol.h
 create mode 100644 drivers/cdx/controller/mcdi.c
 create mode 100644 drivers/cdx/controller/mcdi.h
 create mode 100644 drivers/cdx/controller/mcdi_functions.c
 create mode 100644 drivers/cdx/controller/mcdi_functions.h
 create mode 100644 include/linux/cdx/cdx_bus.h
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v9 1/7 cdx: add the cdx bus driver

Introduce AMD CDX bus, which provides a mechanism for scanning and probing CDX devices. These devices are memory mapped on system bus for Application Processors(APUs).

CDX devices can be changed dynamically in the Fabric and CDX bus interacts with CDX controller to rescan the bus and rediscover the devices.

---
 Documentation/ABI/testing/sysfs-bus-cdx |  12 +
 MAINTAINERS                             |   7 +
 drivers/Kconfig                         |   2 +
 drivers/Makefile                        |   1 +
 drivers/cdx/Kconfig                     |  17 +
 drivers/cdx/Makefile                    |   8 +
 drivers/cdx/cdx.c                       | 407 ++++++++++++++++++++++++
 drivers/cdx/cdx.h                       |  62 ++++
 include/linux/cdx/cdx_bus.h             | 147 +++++++++
 include/linux/mod_devicetable.h         |  15 +
 scripts/mod/devicetable-offsets.c       |   4 +
 scripts/mod/file2alias.c                |  12 +
 12 files changed, 694 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-cdx
 create mode 100644 drivers/cdx/Kconfig
 create mode 100644 drivers/cdx/Makefile
 create mode 100644 drivers/cdx/cdx.c
 create mode 100644 drivers/cdx/cdx.h
 create mode 100644 include/linux/cdx/cdx_bus.h
Documentation/ABI/testing/sysfs-bus-cdx new file +12 -0
new file mode 100644
index 000000000000..063d1a0dd866
@@ -0,0 +1,12 @@
+What:		/sys/bus/cdx/rescan
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Writing a non-zero value to this file cause rescan of the bus
+		and devices on the CDX bus. Any new devices are scanned and
+		added to the list of Linux devices and any devices removed are
+		also deleted from Linux.
+
+		For example::
+
+		  # echo 1 > /sys/bus/cdx/rescan
MAINTAINERS +7 -0
index 631fac6ab555..24f936ad11fd 100644
@@ -964,6 +964,13 @@ Q:	https://patchwork.kernel.org/project/linux-rdma/list/
 F:	drivers/infiniband/hw/efa/
 F:	include/uapi/rdma/efa-abi.h
 
+AMD CDX BUS DRIVER
+M:	Nipun Gupta <[email protected]>
+M:	Nikhil Agarwal <[email protected]>
+S:	Maintained
+F:	drivers/cdx/*
+F:	include/linux/cdx/*
+
 AMD CRYPTOGRAPHIC COPROCESSOR (CCP) DRIVER
 M:	Tom Lendacky <[email protected]>
 M:	John Allen <[email protected]>
drivers/Kconfig +2 -0
index 968bd0a6fd78..514ae6b24cb2 100644
@@ -241,4 +241,6 @@ source "drivers/peci/Kconfig"
 
 source "drivers/hte/Kconfig"
 
+source "drivers/cdx/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index 20b118dca999..7241d80a7b29 100644
@@ -194,3 +194,4 @@ obj-$(CONFIG_MOST)		+= most/
 obj-$(CONFIG_PECI)		+= peci/
 obj-$(CONFIG_HTE)		+= hte/
 obj-$(CONFIG_DRM_ACCEL)		+= accel/
+obj-$(CONFIG_CDX_BUS)		+= cdx/
drivers/cdx/Kconfig new file +17 -0
new file mode 100644
index 000000000000..1aaee0ec5bd9
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# CDX bus configuration
+#
+# Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+#
+
+config CDX_BUS
+	bool "CDX Bus driver"
+	depends on OF && ARM64
+	help
+	  Driver to enable Composable DMA Transfer(CDX) Bus. CDX bus
+	  exposes Fabric devices which uses composable DMA IP to the
+	  APU. CDX bus provides a mechanism for scanning and probing
+	  of CDX devices. CDX devices are memory mapped on system bus
+	  for embedded CPUs. CDX bus uses CDX controller and firmware
+	  to scan these CDX devices.
drivers/cdx/Makefile new file +8 -0
new file mode 100644
index 000000000000..c5feb11fc718
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for CDX
+#
+# Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+#
+
+obj-$(CONFIG_CDX_BUS) += cdx.o
drivers/cdx/cdx.c new file +407 -0

407 lines not on this page · raw message

drivers/cdx/cdx.h new file +62 -0

62 lines not on this page · raw message

include/linux/cdx/cdx_bus.h new file +147 -0

147 lines not on this page · raw message

include/linux/mod_devicetable.h +15 -0
index 549590e9c644..23445b163c2a 100644
@@ -911,4 +911,19 @@ struct ishtp_device_id {
 	kernel_ulong_t driver_data;
 };
 
+/**
+ * struct cdx_device_id - CDX device identifier
+ * @vendor: Vendor ID
+ * @device: Device ID
+ * @override_only: Match only when dev->driver_override is this driver.
+ *
+ * Type of entries in the "device Id" table for CDX devices supported by
+ * a CDX device driver.
+ */
+struct cdx_device_id {
+	__u16 vendor;
+	__u16 device;
+	__u32 override_only;
+};
+
 #endif /* LINUX_MOD_DEVICETABLE_H */
scripts/mod/devicetable-offsets.c +4 -0
index c0d3bcb99138..62dc988df84d 100644
@@ -262,5 +262,9 @@ int main(void)
 	DEVID(ishtp_device_id);
 	DEVID_FIELD(ishtp_device_id, guid);
 
+	DEVID(cdx_device_id);
+	DEVID_FIELD(cdx_device_id, vendor);
+	DEVID_FIELD(cdx_device_id, device);
+
 	return 0;
 }
scripts/mod/file2alias.c +12 -0
index 91c2e7ba5e52..28da34ba4359 100644
@@ -1452,6 +1452,17 @@ static int do_dfl_entry(const char *filename, void *symval, char *alias)
 	return 1;
 }
 
+/* Looks like: cdx:vNdN */
+static int do_cdx_entry(const char *filename, void *symval,
+			char *alias)
+{
+	DEF_FIELD(symval, cdx_device_id, vendor);
+	DEF_FIELD(symval, cdx_device_id, device);
+
+	sprintf(alias, "cdx:v%08Xd%08Xd", vendor, device);
+	return 1;
+}
+
 /* Does namelen bytes of name exactly match the symbol? */
 static bool sym_is(const char *name, unsigned namelen, const char *symbol)
 {
@@ -1531,6 +1542,7 @@ static const struct devtable devtable[] = {
 	{"ssam", SIZE_ssam_device_id, do_ssam_entry},
 	{"dfl", SIZE_dfl_device_id, do_dfl_entry},
 	{"ishtp", SIZE_ishtp_device_id, do_ishtp_entry},
+	{"cdx", SIZE_cdx_device_id, do_cdx_entry},
 };
 
 /* Create MODULE_ALIAS() statements.
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v9 1/7] cdx: add the cdx bus driver

Quoted 40 lines · Nipun Gupta

Introduce AMD CDX bus, which provides a mechanism for scanning and probing CDX devices. These devices are memory mapped on system bus for Application Processors(APUs).

CDX devices can be changed dynamically in the Fabric and CDX bus interacts with CDX controller to rescan the bus and rediscover the devices.

---
 Documentation/ABI/testing/sysfs-bus-cdx |  12 +
 MAINTAINERS                             |   7 +
 drivers/Kconfig                         |   2 +
 drivers/Makefile                        |   1 +
 drivers/cdx/Kconfig                     |  17 +
 drivers/cdx/Makefile                    |   8 +
 drivers/cdx/cdx.c                       | 407 ++++++++++++++++++++++++
 drivers/cdx/cdx.h                       |  62 ++++
 include/linux/cdx/cdx_bus.h             | 147 +++++++++
 include/linux/mod_devicetable.h         |  15 +
 scripts/mod/devicetable-offsets.c       |   4 +
 scripts/mod/file2alias.c                |  12 +
 12 files changed, 694 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-cdx
 create mode 100644 drivers/cdx/Kconfig
 create mode 100644 drivers/cdx/Makefile
 create mode 100644 drivers/cdx/cdx.c
 create mode 100644 drivers/cdx/cdx.h
 create mode 100644 include/linux/cdx/cdx_bus.h
Documentation/ABI/testing/sysfs-bus-cdx new file +2 -0
new file mode 100644
index 000000000000..063d1a0dd866
@@ -0,0 +1,12 @@
+What:		/sys/bus/cdx/rescan
+Date:		March 2023

Documentation/ABI/testing/sysfs-bus-cdx line 12

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-cdx new file +10 -0
+Contact:	[email protected]
+Description:
+		Writing a non-zero value to this file cause rescan of the bus
+		and devices on the CDX bus. Any new devices are scanned and
+		added to the list of Linux devices and any devices removed are
+		also deleted from Linux.
+
+		For example::
+
+		  # echo 1 > /sys/bus/cdx/rescan

See my comments on patch 7/7 about "non-zero" not being a good idea.

thanks,

greg k-h

author maintainer
#3 lore raw

PATCH v9 2/7 iommu: Support ops registration for CDX bus

In reply to Nipun Gupta

With new CDX bus supported for AMD FPGA devices on ARM platform, the bus requires registration for the SMMU v3 driver.

---
 drivers/iommu/iommu.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iommu/iommu.c +4 -0
index 10db680acaed..0025a631b8e1 100644
@@ -28,6 +28,7 @@
 #include <linux/fsl/mc.h>
 #include <linux/module.h>
 #include <linux/cc_platform.h>
+#include <linux/cdx/cdx_bus.h>
 #include <trace/events/iommu.h>
 #include <linux/sched/mm.h>
 #include <linux/msi.h>
@@ -129,6 +130,9 @@ static struct bus_type * const iommu_buses[] = {
 #ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS
 	&host1x_context_device_bus_type,
 #endif
+#ifdef CONFIG_CDX_BUS
+	&cdx_bus_type,
+#endif
 };
 
 /*
Signature · 1 line
2.17.1
Reviewed-by on v9 2/7
#4 lore raw

Re: [PATCH v9 2/7] iommu: Support ops registration for CDX bus

Quoted 7 lines · Nipun Gupta

With new CDX bus supported for AMD FPGA devices on ARM platform, the bus requires registration for the SMMU v3 driver.

Nit:

The bus iommu ops is about to retire. So probably the commit title could be "iommu: Add iommu probe for CDX bus"?

Anyway,

Best regards,
baolu

author maintainer
#5 lore raw

PATCH v9 3/7 dt-bindings: bus: add CDX bus controller for versal net

In reply to Nipun Gupta

Add CDX bus controller device tree bindings for versal-net devices.

---
 .../bindings/bus/xlnx,versal-net-cdx.yaml     | 82 +++++++++++++++++++
 MAINTAINERS                                   |  1 +
 2 files changed, 83 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/bus/xlnx,versal-net-cdx.yaml
Documentation/devicetree/bindings/bus/xlnx,versal-net-cdx.yaml new file +82 -0
new file mode 100644
index 000000000000..7f62ffbdc245
@@ -0,0 +1,82 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/bus/xlnx,versal-net-cdx.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: AMD CDX bus controller
+
+description: |
+  CDX bus controller for AMD devices is implemented to dynamically
+  detect CDX bus and devices using the firmware.
+  The CDX bus manages multiple FPGA based hardware devices, which
+  can support network, crypto or any other specialized type of
+  devices. These FPGA based devices can be added/modified dynamically
+  on run-time.
+
+  All devices on the CDX bus will have a unique streamid (for IOMMU)
+  and a unique device ID (for MSI) corresponding to a requestor ID
+  (one to one associated with the device). The streamid and deviceid
+  are used to configure SMMU and GIC-ITS respectively.
+
+  iommu-map property is used to define the set of stream ids
+  corresponding to each device and the associated IOMMU.
+
+  The MSI writes are accompanied by sideband data (Device ID).
+  The msi-map property is used to associate the devices with the
+  device ID as well as the associated ITS controller.
+
+  rproc property (xlnx,rproc) is used to identify the remote processor
+  with which APU (Application Processor Unit) interacts to find out
+  the bus and device configuration.
+
+maintainers:
+  - Nipun Gupta <[email protected]>
+  - Nikhil Agarwal <[email protected]>
+
+properties:
+  compatible:
+    const: xlnx,versal-net-cdx
+
+  iommu-map: true
+
+  msi-map: true
+
+  xlnx,rproc:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description:
+      phandle to the remoteproc_r5 rproc node using which APU interacts
+      with remote processor.
+
+  ranges: true
+
+  "#address-cells":
+    enum: [1, 2]
+
+  "#size-cells":
+    enum: [1, 2]
+
+required:
+  - compatible
+  - iommu-map
+  - msi-map
+  - xlnx,rproc
+  - ranges
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    cdx {
+        compatible = "xlnx,versal-net-cdx";
+        #address-cells = <1>;
+        #size-cells = <1>;
+        /* define map for RIDs 250-259 */
+        iommu-map = <250 &smmu 250 10>;
+        /* define msi map for RIDs 250-259 */
+        msi-map = <250 &its 250 10>;
+        xlnx,rproc = <&remoteproc_r5>;
+        ranges;
+    };
MAINTAINERS +1 -0
index 24f936ad11fd..3174deca545c 100644
@@ -968,6 +968,7 @@ AMD CDX BUS DRIVER
 M:	Nipun Gupta <[email protected]>
 M:	Nikhil Agarwal <[email protected]>
 S:	Maintained
+F:	Documentation/devicetree/bindings/bus/xlnx,versal-net-cdx.yaml
 F:	drivers/cdx/*
 F:	include/linux/cdx/*
 
Signature · 1 line
2.17.1
author maintainer
#6 lore raw

PATCH v9 4/7 cdx: add MCDI protocol interface for firmware interaction

In reply to Nipun Gupta

The MCDI (Management CPU Driver Interface) is used as a protocol to communicate with the RPU firmware. It has pre-defined set of messages for different message exchanges between APU and RPU.

---
 drivers/cdx/Kconfig                  |   2 +
 drivers/cdx/Makefile                 |   2 +-
 drivers/cdx/controller/Kconfig       |  20 +
 drivers/cdx/controller/Makefile      |   9 +
 drivers/cdx/controller/bitfield.h    |  90 +++
 drivers/cdx/controller/mc_cdx_pcol.h | 590 +++++++++++++++++
 drivers/cdx/controller/mcdi.c        | 903 +++++++++++++++++++++++++++
 drivers/cdx/controller/mcdi.h        | 238 +++++++
 8 files changed, 1853 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cdx/controller/Kconfig
 create mode 100644 drivers/cdx/controller/Makefile
 create mode 100644 drivers/cdx/controller/bitfield.h
 create mode 100644 drivers/cdx/controller/mc_cdx_pcol.h
 create mode 100644 drivers/cdx/controller/mcdi.c
 create mode 100644 drivers/cdx/controller/mcdi.h
drivers/cdx/Kconfig +2 -0
index 1aaee0ec5bd9..a08958485e31 100644
@@ -15,3 +15,5 @@ config CDX_BUS
 	  of CDX devices. CDX devices are memory mapped on system bus
 	  for embedded CPUs. CDX bus uses CDX controller and firmware
 	  to scan these CDX devices.
+
+source "drivers/cdx/controller/Kconfig"
drivers/cdx/Makefile +1 -1
index c5feb11fc718..0324e4914f6e 100644
@@ -5,4 +5,4 @@
 # Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
 #
 
-obj-$(CONFIG_CDX_BUS) += cdx.o
+obj-$(CONFIG_CDX_BUS) += cdx.o controller/
drivers/cdx/controller/Kconfig new file +20 -0
new file mode 100644
index 000000000000..785c71063b2a
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# CDX controller configuration
+#
+# Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+#
+
+if CDX_BUS
+
+config MCDI_LOGGING
+	bool "MCDI Logging for the CDX controller"
+	depends on CDX_CONTROLLER
+	help
+	  Enable MCDI Logging for
+	  the CDX Controller for debug
+	  purpose.
+
+	  If unsure, say N.
+
+endif
drivers/cdx/controller/Makefile new file +9 -0
new file mode 100644
index 000000000000..0ce200678eda
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Makefile for CDX controller drivers
+#
+# Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+#
+
+obj-$(CONFIG_CDX_CONTROLLER) += cdx-controller.o
+cdx-controller-objs := mcdi.o
drivers/cdx/controller/bitfield.h new file +90 -0

90 lines not on this page · raw message

drivers/cdx/controller/mc_cdx_pcol.h new file +590 -0

590 lines not on this page · raw message

drivers/cdx/controller/mcdi.c new file +903 -0

903 lines not on this page · raw message

drivers/cdx/controller/mcdi.h new file +238 -0

238 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#7 lore raw

PATCH v9 5/7 cdx: add cdx controller

In reply to Nipun Gupta

CDX controller uses MCDI interface as a protocol to communicate with the RPU firmware and registers the detected CDX devices on the CDX bus. It also uses RPMsg as the communication channel with the Firmware.

---
 drivers/cdx/controller/Kconfig          |   9 ++
 drivers/cdx/controller/Makefile         |   2 +-
 drivers/cdx/controller/cdx_controller.c | 188 ++++++++++++++++++++++++
 drivers/cdx/controller/mcdi_functions.c | 125 ++++++++++++++++
 drivers/cdx/controller/mcdi_functions.h |  50 +++++++
 5 files changed, 373 insertions(+), 1 deletion(-)
 create mode 100644 drivers/cdx/controller/cdx_controller.c
 create mode 100644 drivers/cdx/controller/mcdi_functions.c
 create mode 100644 drivers/cdx/controller/mcdi_functions.h
drivers/cdx/controller/Kconfig +9 -0
index 785c71063b2a..17f9c6be2fe1 100644
@@ -7,6 +7,15 @@
 
 if CDX_BUS
 
+config CDX_CONTROLLER
+	tristate "CDX bus controller"
+	help
+	  CDX controller drives the CDX bus. It interacts with
+	  firmware to get the hardware devices and registers with
+	  the CDX bus. Say Y to enable the CDX hardware driver.
+
+	  If unsure, say N.
+
 config MCDI_LOGGING
 	bool "MCDI Logging for the CDX controller"
 	depends on CDX_CONTROLLER
drivers/cdx/controller/Makefile +1 -1
index 0ce200678eda..f7437c882cc9 100644
@@ -6,4 +6,4 @@
 #
 
 obj-$(CONFIG_CDX_CONTROLLER) += cdx-controller.o
-cdx-controller-objs := mcdi.o
+cdx-controller-objs := cdx_controller.o mcdi.o mcdi_functions.o
drivers/cdx/controller/cdx_controller.c new file +188 -0

188 lines not on this page · raw message

drivers/cdx/controller/mcdi_functions.c new file +125 -0

125 lines not on this page · raw message

drivers/cdx/controller/mcdi_functions.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#8 lore raw

PATCH v9 6/7 cdx: add rpmsg communication channel for CDX

In reply to Nipun Gupta

From: Abhijit Gangurde <[email protected]>

RPMsg is used as a transport communication channel. This change introduces RPMsg driver and integrates it with the CDX controller.

---
 drivers/cdx/controller/Kconfig          |   2 +
 drivers/cdx/controller/Makefile         |   2 +-
 drivers/cdx/controller/cdx_controller.c |  32 +++-
 drivers/cdx/controller/cdx_controller.h |  30 ++++
 drivers/cdx/controller/cdx_rpmsg.c      | 202 ++++++++++++++++++++++++
 drivers/cdx/controller/mcdi.h           |  10 ++
 6 files changed, 273 insertions(+), 5 deletions(-)
 create mode 100644 drivers/cdx/controller/cdx_controller.h
 create mode 100644 drivers/cdx/controller/cdx_rpmsg.c
drivers/cdx/controller/Kconfig +2 -0
index 17f9c6be2fe1..c3e3b9ff8dfe 100644
@@ -9,6 +9,8 @@ if CDX_BUS
 
 config CDX_CONTROLLER
 	tristate "CDX bus controller"
+	select REMOTEPROC
+	select RPMSG
 	help
 	  CDX controller drives the CDX bus. It interacts with
 	  firmware to get the hardware devices and registers with
drivers/cdx/controller/Makefile +1 -1
index f7437c882cc9..f071be411d96 100644
@@ -6,4 +6,4 @@
 #
 
 obj-$(CONFIG_CDX_CONTROLLER) += cdx-controller.o
-cdx-controller-objs := cdx_controller.o mcdi.o mcdi_functions.o
+cdx-controller-objs := cdx_controller.o cdx_rpmsg.o mcdi.o mcdi_functions.o
drivers/cdx/controller/cdx_controller.c +28 -4
index fbebc8cdcbf8..0d1826980935 100644
@@ -9,6 +9,7 @@
 #include <linux/slab.h>
 #include <linux/cdx/cdx_bus.h>
 
+#include "cdx_controller.h"
 #include "../cdx.h"
 #include "mcdi_functions.h"
 #include "mcdi.h"
@@ -22,10 +23,8 @@ static void cdx_mcdi_request(struct cdx_mcdi *cdx,
 			     const struct cdx_dword *hdr, size_t hdr_len,
 			     const struct cdx_dword *sdu, size_t sdu_len)
 {
-	/*
-	 * This will get updated by rpmsg APIs, with RPMSG introduction
-	 * in CDX controller as a transport layer.
-	 */
+	if (cdx_rpmsg_send(cdx, hdr, hdr_len, sdu, sdu_len))
+		dev_err(&cdx->rpdev->dev, "Failed to send rpmsg data\n");
 }
 
 static const struct cdx_mcdi_ops mcdi_ops = {
@@ -33,6 +32,19 @@ static const struct cdx_mcdi_ops mcdi_ops = {
 	.mcdi_request = cdx_mcdi_request,
 };
 
+void cdx_rpmsg_post_probe(struct cdx_controller *cdx)
+{
+	/* Register CDX controller with CDX bus driver */
+	if (cdx_register_controller(cdx))
+		dev_err(cdx->dev, "Failed to register CDX controller\n");
+}
+
+void cdx_rpmsg_pre_remove(struct cdx_controller *cdx)
+{
+	cdx_unregister_controller(cdx);
+	cdx_mcdi_wait_for_quiescence(cdx->priv, MCDI_RPC_TIMEOUT);
+}
+
 static int cdx_scan_devices(struct cdx_controller *cdx)
 {
 	struct cdx_mcdi *cdx_mcdi = cdx->priv;
@@ -124,8 +136,18 @@ static int xlnx_cdx_probe(struct platform_device *pdev)
 	cdx->priv = cdx_mcdi;
 	cdx->ops = &cdx_ops;
 
+	ret = cdx_setup_rpmsg(pdev);
+	if (ret) {
+		if (ret != -EPROBE_DEFER)
+			dev_err(&pdev->dev, "Failed to register CDX RPMsg transport\n");
+		goto cdx_rpmsg_fail;
+	}
+
+	dev_info(&pdev->dev, "Successfully registered CDX controller with RPMsg as transport\n");
 	return 0;
 
+cdx_rpmsg_fail:
+	kfree(cdx);
 cdx_alloc_fail:
 	cdx_mcdi_finish(cdx_mcdi);
 mcdi_init_fail:
@@ -139,6 +161,8 @@ static int xlnx_cdx_remove(struct platform_device *pdev)
 	struct cdx_controller *cdx = platform_get_drvdata(pdev);
 	struct cdx_mcdi *cdx_mcdi = cdx->priv;
 
+	cdx_destroy_rpmsg(pdev);
+
 	kfree(cdx);
 
 	cdx_mcdi_finish(cdx_mcdi);
drivers/cdx/controller/cdx_controller.h new file +30 -0
new file mode 100644
index 000000000000..43b7c742df87
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Header file for the CDX Controller
+ *
+ * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _CDX_CONTROLLER_H_
+#define _CDX_CONTROLLER_H_
+
+#include <linux/cdx/cdx_bus.h>
+#include "mcdi_functions.h"
+
+void cdx_rpmsg_post_probe(struct cdx_controller *cdx);
+
+void cdx_rpmsg_pre_remove(struct cdx_controller *cdx);
+
+int cdx_rpmsg_send(struct cdx_mcdi *cdx_mcdi,
+		   const struct cdx_dword *hdr, size_t hdr_len,
+		   const struct cdx_dword *sdu, size_t sdu_len);
+
+void cdx_rpmsg_read_resp(struct cdx_mcdi *cdx_mcdi,
+			 struct cdx_dword *outbuf, size_t offset,
+			 size_t outlen);
+
+int cdx_setup_rpmsg(struct platform_device *pdev);
+
+void cdx_destroy_rpmsg(struct platform_device *pdev);
+
+#endif /* _CDX_CONT_PRIV_H_ */
drivers/cdx/controller/cdx_rpmsg.c new file +202 -0

202 lines not on this page · raw message

drivers/cdx/controller/mcdi.h +10 -0
index 63933ede33ed..0bfbeab04e43 100644
@@ -9,6 +9,7 @@
 
 #include <linux/mutex.h>
 #include <linux/kref.h>
+#include <linux/rpmsg.h>
 
 #include "bitfield.h"
 #include "mc_cdx_pcol.h"
@@ -62,11 +63,20 @@ enum cdx_mcdi_cmd_state {
  *	with CDX controller.
  * @mcdi: MCDI interface
  * @mcdi_ops: MCDI operations
+ * @r5_rproc : R5 Remoteproc device handle
+ * @rpdev: RPMsg device
+ * @ept: RPMsg endpoint
+ * @work: Post probe work
  */
 struct cdx_mcdi {
 	/* MCDI interface */
 	struct cdx_mcdi_data *mcdi;
 	const struct cdx_mcdi_ops *mcdi_ops;
+
+	struct rproc *r5_rproc;
+	struct rpmsg_device *rpdev;
+	struct rpmsg_endpoint *ept;
+	struct work_struct work;
 };
 
 struct cdx_mcdi_ops {
Signature · 1 line
2.17.1
author maintainer
#9 lore raw

PATCH v9 7/7 cdx: add device attributes

In reply to Nipun Gupta

Create sysfs entry for CDX devices.

Sysfs entries provided in each of the CDX device detected by the CDX controller
- vendor id
- device id
- remove
- reset of the device.
- driver override

---
 Documentation/ABI/testing/sysfs-bus-cdx |  44 +++++++
 drivers/cdx/cdx.c                       | 145 ++++++++++++++++++++++++
 drivers/cdx/controller/cdx_controller.c |  19 ++++
 drivers/cdx/controller/mcdi_functions.c |  14 +++
 drivers/cdx/controller/mcdi_functions.h |  11 ++
 include/linux/cdx/cdx_bus.h             |  27 +++++
 6 files changed, 260 insertions(+)
Documentation/ABI/testing/sysfs-bus-cdx +44 -0
index 063d1a0dd866..c553ce3a449a 100644
@@ -10,3 +10,47 @@ Description:
 		For example::
 
 		  # echo 1 > /sys/bus/cdx/rescan
+
+What:		/sys/bus/cdx/devices/.../vendor
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Vendor ID for this CDX device. Vendor ID is 16 bit
+		identifier which is specific to the device manufacturer.
+		Combination of Vendor ID and Device ID identifies a device.
+
+What:		/sys/bus/cdx/devices/.../device
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Device ID for this CDX device. Device ID is a 16 bit
+		identifier to identify a device type within the range
+		of a device manufacturer.
+		Combination of Vendor ID and Device ID identifies a device.
+
+What:		/sys/bus/cdx/devices/.../reset
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Writing a non-zero value to this file resets the CDX device.
+		On resetting the device, the corresponding driver is notified
+		twice, once before the device is being reset, and again after
+		the reset has been complete.
+
+		For example::
+
+		  # echo 1 > /sys/bus/cdx/.../reset
+
+What:		/sys/bus/cdx/devices/.../remove
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Writing a non-zero value to this file removes the corresponding
+		device from the CDX bus. If the device is to be reconfigured
+		reconfigured in the Hardware, the device can be removed, so
+		that the device driver does not access the device while it is
+		being reconfigured.
+
+		For example::
+
+		  # echo 1 > /sys/bus/cdx/devices/.../remove
drivers/cdx/cdx.c +145 -0

163 lines not on this page · raw message

drivers/cdx/controller/cdx_controller.c +19 -0
index 0d1826980935..b1c2e96589d1 100644
@@ -45,6 +45,24 @@ void cdx_rpmsg_pre_remove(struct cdx_controller *cdx)
 	cdx_mcdi_wait_for_quiescence(cdx->priv, MCDI_RPC_TIMEOUT);
 }
 
+static int cdx_configure_device(struct cdx_controller *cdx,
+				u8 bus_num, u8 dev_num,
+				struct cdx_device_config *dev_config)
+{
+	int ret = 0;
+
+	switch (dev_config->type) {
+	case CDX_DEV_RESET_CONF:
+		ret = cdx_mcdi_reset_device(cdx->priv, bus_num, dev_num);
+		break;
+	default:
+		dev_err(cdx->dev, "Invalid device configuration flag\n");
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
 static int cdx_scan_devices(struct cdx_controller *cdx)
 {
 	struct cdx_mcdi *cdx_mcdi = cdx->priv;
@@ -104,6 +122,7 @@ static int cdx_scan_devices(struct cdx_controller *cdx)
 
 static struct cdx_ops cdx_ops = {
 	.scan		= cdx_scan_devices,
+	.dev_configure	= cdx_configure_device,
 };
 
 static int xlnx_cdx_probe(struct platform_device *pdev)
drivers/cdx/controller/mcdi_functions.c +14 -0
index 012b52881dd5..0158f26533dd 100644
@@ -123,3 +123,17 @@ int cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
 
 	return 0;
 }
+
+int cdx_mcdi_reset_device(struct cdx_mcdi *cdx, u8 bus_num, u8 dev_num)
+{
+	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_RESET_IN_LEN);
+	int ret;
+
+	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_BUS, bus_num);
+	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_DEVICE, dev_num);
+
+	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_RESET, inbuf, sizeof(inbuf),
+			   NULL, 0, NULL);
+
+	return ret;
+}
drivers/cdx/controller/mcdi_functions.h +11 -0
index 6bf5a4a0778f..7440ace5539a 100644
@@ -47,4 +47,15 @@ int cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
 			    u8 bus_num, u8 dev_num,
 			    struct cdx_dev_params *dev_params);
 
+/**
+ * cdx_mcdi_reset_device - Reset cdx device represented by bus_num:dev_num
+ * @cdx: pointer to MCDI interface.
+ * @bus_num: Bus number.
+ * @dev_num: Device number.
+ *
+ * Return: 0 on success, <0 on failure
+ */
+int cdx_mcdi_reset_device(struct cdx_mcdi *cdx,
+			  u8 bus_num, u8 dev_num);
+
 #endif /* CDX_MCDI_FUNCTIONS_H */
include/linux/cdx/cdx_bus.h +27 -0
index d134e0104724..35ef41d8a61a 100644
@@ -21,8 +21,20 @@
 /* Forward declaration for CDX controller */
 struct cdx_controller;
 
+enum {
+	CDX_DEV_RESET_CONF,
+};
+
+struct cdx_device_config {
+	u8 type;
+};
+
 typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
 
+typedef int (*cdx_dev_configure_cb)(struct cdx_controller *cdx,
+				    u8 bus_num, u8 dev_num,
+				    struct cdx_device_config *dev_config);
+
 /**
  * CDX_DEVICE_DRIVER_OVERRIDE - macro used to describe a CDX device with
  *                              override_only flags.
@@ -39,9 +51,12 @@ typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
 /**
  * struct cdx_ops - Callbacks supported by CDX controller.
  * @scan: scan the devices on the controller
+ * @dev_configure: configuration like reset, master_enable,
+ *		   msi_config etc for a CDX device
  */
 struct cdx_ops {
 	cdx_scan_cb scan;
+	cdx_dev_configure_cb dev_configure;
 };
 
 /**
@@ -101,6 +116,8 @@ struct cdx_device {
  * @probe: Function called when a device is added
  * @remove: Function called when a device is removed
  * @shutdown: Function called at shutdown time to quiesce the device
+ * @reset_prepare: Function called before is reset to notify driver
+ * @reset_done: Function called after reset is complete to notify driver
  * @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA.
  *		For most device drivers, no need to care about this flag
  *		as long as all DMAs are handled through the kernel DMA API.
@@ -115,6 +132,8 @@ struct cdx_driver {
 	int (*probe)(struct cdx_device *dev);
 	int (*remove)(struct cdx_device *dev);
 	void (*shutdown)(struct cdx_device *dev);
+	void (*reset_prepare)(struct cdx_device *dev);
+	void (*reset_done)(struct cdx_device *dev);
 	bool driver_managed_dma;
 };
 
@@ -144,4 +163,12 @@ void cdx_driver_unregister(struct cdx_driver *cdx_driver);
 
 extern struct bus_type cdx_bus_type;
 
+/**
+ * cdx_dev_reset - Reset CDX device
+ * @dev: device pointer
+ *
+ * Return: 0 for success, -errno on failure
+ */
+int cdx_dev_reset(struct device *dev);
+
 #endif /* _CDX_BUS_H_ */
Signature · 1 line
2.17.1
#10 lore raw

Re: [PATCH v9 7/7] cdx: add device attributes

Quoted 39 lines · Nipun Gupta

Create sysfs entry for CDX devices.

Sysfs entries provided in each of the CDX device detected by the CDX controller
- vendor id
- device id
- remove
- reset of the device.
- driver override

---
 Documentation/ABI/testing/sysfs-bus-cdx |  44 +++++++
 drivers/cdx/cdx.c                       | 145 ++++++++++++++++++++++++
 drivers/cdx/controller/cdx_controller.c |  19 ++++
 drivers/cdx/controller/mcdi_functions.c |  14 +++
 drivers/cdx/controller/mcdi_functions.h |  11 ++
 include/linux/cdx/cdx_bus.h             |  27 +++++
 6 files changed, 260 insertions(+)
Documentation/ABI/testing/sysfs-bus-cdx +7 -0
index 063d1a0dd866..c553ce3a449a 100644
@@ -10,3 +10,47 @@ Description:
 		For example::
 
 		  # echo 1 > /sys/bus/cdx/rescan
+
+What:		/sys/bus/cdx/devices/.../vendor
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Vendor ID for this CDX device. Vendor ID is 16 bit
+		identifier which is specific to the device manufacturer.

Documentation/ABI/testing/sysfs-bus-cdx Description: · line 29

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-cdx +10 -0
+		Combination of Vendor ID and Device ID identifies a device.
+
+What:		/sys/bus/cdx/devices/.../device
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Device ID for this CDX device. Device ID is a 16 bit
+		identifier to identify a device type within the range
+		of a device manufacturer.
+		Combination of Vendor ID and Device ID identifies a device.

Are these printed out in hex, decimal, octal, binary, something else? Please be specific.

Documentation/ABI/testing/sysfs-bus-cdx Description: · line 42

Quoted 13 lines
Documentation/ABI/testing/sysfs-bus-cdx +13 -0
+
+What:		/sys/bus/cdx/devices/.../reset
+Date:		March 2023
+Contact:	[email protected]
+Description:
+		Writing a non-zero value to this file resets the CDX device.
+		On resetting the device, the corresponding driver is notified
+		twice, once before the device is being reset, and again after
+		the reset has been complete.
+
+		For example::
+
+		  # echo 1 > /sys/bus/cdx/.../reset

Don't do "non-zero", make this explicit. This implies I can do "echo X" and it will work, do you want that?

Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-cdx +4 -0
+
+What:		/sys/bus/cdx/devices/.../remove
+Date:		March 2023
+Contact:	[email protected]

Documentation/ABI/testing/sysfs-bus-cdx Description: · line 56

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-cdx +10 -0
+Description:
+		Writing a non-zero value to this file removes the corresponding
+		device from the CDX bus. If the device is to be reconfigured
+		reconfigured in the Hardware, the device can be removed, so
+		that the device driver does not access the device while it is
+		being reconfigured.
+
+		For example::
+
+		  # echo 1 > /sys/bus/cdx/devices/.../remove

Again, not non-zero.

Please use the built-in sysfs function to handle yes/no values being written to sysfs files for this and only accept the "yes" value.

Quoted 20 lines
drivers/cdx/cdx.c +12 -0
index c981232486dd..0c4f300373e5 100644
@@ -71,6 +71,39 @@
 /* CDX controllers registered with the CDX bus */
 static DEFINE_XARRAY_ALLOC(cdx_controllers);
 
+/**
+ * cdx_dev_reset - Reset a CDX device
+ * @dev: CDX device
+ *
+ * Return: -errno on failure, 0 on success.
+ */
+int cdx_dev_reset(struct device *dev)
+{
+	struct cdx_device *cdx_dev = to_cdx_device(dev);
+	struct cdx_controller *cdx = cdx_dev->cdx;
+	struct cdx_device_config dev_config;
+	struct cdx_driver *cdx_drv;

drivers/cdx/cdx.c line 95

Quoted 10 lines
drivers/cdx/cdx.c +10 -0
+	int ret;
+
+	cdx_drv = to_cdx_driver(dev->driver);
+	/* Notify driver that device is being reset */
+	if (cdx_drv && cdx_drv->reset_prepare)
+		cdx_drv->reset_prepare(cdx_dev);
+
+	dev_config.type = CDX_DEV_RESET_CONF;
+	ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
+				      cdx_dev->dev_num, &dev_config);

You configure something off of the stack? Is that wise, it contains loads of invalid stack-provided data, how does this work at all?

Quoted 26 lines
drivers/cdx/cdx.c +19 -0
+	if (ret)
+		dev_err(dev, "cdx device reset failed\n");
+
+	/* Notify driver that device reset is complete */
+	if (cdx_drv && cdx_drv->reset_done)
+		cdx_drv->reset_done(cdx_dev);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(cdx_dev_reset);
+
 /**
  * cdx_unregister_device - Unregister a CDX device
  * @dev: CDX device
@@ -237,6 +270,117 @@ static int cdx_dma_configure(struct device *dev)
 	return 0;
 }
 
+/* show configuration fields */
+#define cdx_config_attr(field, format_string)	\
+static ssize_t	\
+field##_show(struct device *dev, struct device_attribute *attr, char *buf)	\
+{	\
+	struct cdx_device *cdx_dev = to_cdx_device(dev);	\
+	return sysfs_emit(buf, format_string, cdx_dev->field);	\
+}	\

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev) · line 290

Quoted 10 lines
drivers/cdx/cdx.c +10 -0
+static DEVICE_ATTR_RO(field)
+
+cdx_config_attr(vendor, "0x%04x\n");
+cdx_config_attr(device, "0x%04x\n");
+
+static ssize_t remove_store(struct device *dev,
+			    struct device_attribute *attr,
+			    const char *buf, size_t count)
+{
+	unsigned long val = 0;

Why initialize this?

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev) · line 293

Quoted 3 lines
drivers/cdx/cdx.c +3 -0
+
+	if (kstrtoul(buf, 0, &val) < 0)
+		return -EINVAL;

As mentioned above, just use kstrtobool() instead, don't mess around with unsigned longs, that's not needed at all.

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev) · line 342

Quoted 13 lines
drivers/cdx/cdx.c +13 -0
+static ssize_t driver_override_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct cdx_device *cdx_dev = to_cdx_device(dev);
+	const char *old = cdx_dev->driver_override;
+	char *driver_override;
+	char *cp;
+
+	if (WARN_ON(dev->bus != &cdx_bus_type))
+		return -EINVAL;
+
+	if (count >= (PAGE_SIZE - 1))

And how can that happen?

And why does the page size matter?

Quoted 25 lines
drivers/cdx/cdx.c +25 -0
+		return -EINVAL;
+
+	driver_override = kstrndup(buf, count, GFP_KERNEL);
+	if (!driver_override)
+		return -ENOMEM;
+
+	cp = strchr(driver_override, '\n');
+	if (cp)
+		*cp = '\0';
+
+	if (strlen(driver_override)) {
+		cdx_dev->driver_override = driver_override;
+	} else {
+		kfree(driver_override);
+		cdx_dev->driver_override = NULL;
+	}
+
+	kfree(old);
+
+	return count;
+}
+
+static int cdx_configure_device(struct cdx_controller *cdx,
+				u8 bus_num, u8 dev_num,
+				struct cdx_device_config *dev_config)

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev)

Quoted 10 lines
drivers/cdx/cdx.c +10 -0
+{
+	int ret = 0;
+
+	switch (dev_config->type) {
+	case CDX_DEV_RESET_CONF:
+		ret = cdx_mcdi_reset_device(cdx->priv, bus_num, dev_num);
+		break;
+	default:
+		dev_err(cdx->dev, "Invalid device configuration flag\n");
+		ret = -EINVAL;

Can userspace cause this to happen? If so, please do not allow it to spam the kernel log.

If not, who will see this error?

Quoted 106 lines
drivers/cdx/cdx.c +6 -0
+	}
+
+	return ret;
+}
+
 static int cdx_scan_devices(struct cdx_controller *cdx)
 {
 	struct cdx_mcdi *cdx_mcdi = cdx->priv;
@@ -104,6 +122,7 @@ static int cdx_scan_devices(struct cdx_controller *cdx)
 
 static struct cdx_ops cdx_ops = {
 	.scan		= cdx_scan_devices,
+	.dev_configure	= cdx_configure_device,
 };
 
 static int xlnx_cdx_probe(struct platform_device *pdev)
drivers/cdx/controller/mcdi_functions.c +14 -0
index 012b52881dd5..0158f26533dd 100644
@@ -123,3 +123,17 @@ int cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
 
 	return 0;
 }
+
+int cdx_mcdi_reset_device(struct cdx_mcdi *cdx, u8 bus_num, u8 dev_num)
+{
+	MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_RESET_IN_LEN);
+	int ret;
+
+	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_BUS, bus_num);
+	MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_DEVICE, dev_num);
+
+	ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_RESET, inbuf, sizeof(inbuf),
+			   NULL, 0, NULL);
+
+	return ret;
+}
drivers/cdx/controller/mcdi_functions.h +11 -0
index 6bf5a4a0778f..7440ace5539a 100644
@@ -47,4 +47,15 @@ int cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
 			    u8 bus_num, u8 dev_num,
 			    struct cdx_dev_params *dev_params);
 
+/**
+ * cdx_mcdi_reset_device - Reset cdx device represented by bus_num:dev_num
+ * @cdx: pointer to MCDI interface.
+ * @bus_num: Bus number.
+ * @dev_num: Device number.
+ *
+ * Return: 0 on success, <0 on failure
+ */
+int cdx_mcdi_reset_device(struct cdx_mcdi *cdx,
+			  u8 bus_num, u8 dev_num);
+
 #endif /* CDX_MCDI_FUNCTIONS_H */
include/linux/cdx/cdx_bus.h +17 -0
index d134e0104724..35ef41d8a61a 100644
@@ -21,8 +21,20 @@
 /* Forward declaration for CDX controller */
 struct cdx_controller;
 
+enum {
+	CDX_DEV_RESET_CONF,
+};
+
+struct cdx_device_config {
+	u8 type;
+};
+
 typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
 
+typedef int (*cdx_dev_configure_cb)(struct cdx_controller *cdx,
+				    u8 bus_num, u8 dev_num,
+				    struct cdx_device_config *dev_config);
+
 /**
  * CDX_DEVICE_DRIVER_OVERRIDE - macro used to describe a CDX device with
  *                              override_only flags.
@@ -39,9 +51,12 @@ typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
 /**
  * struct cdx_ops - Callbacks supported by CDX controller.
  * @scan: scan the devices on the controller
+ * @dev_configure: configuration like reset, master_enable,
+ *		   msi_config etc for a CDX device
  */
 struct cdx_ops {
 	cdx_scan_cb scan;
+	cdx_dev_configure_cb dev_configure;
 };
 
 /**
@@ -101,6 +116,8 @@ struct cdx_device {
  * @probe: Function called when a device is added
  * @remove: Function called when a device is removed
  * @shutdown: Function called at shutdown time to quiesce the device
+ * @reset_prepare: Function called before is reset to notify driver
+ * @reset_done: Function called after reset is complete to notify driver
  * @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA.
  *		For most device drivers, no need to care about this flag
  *		as long as all DMAs are handled through the kernel DMA API.

include/linux/cdx/cdx_bus.h struct cdx_driver { · line 135

Quoted 4 lines
include/linux/cdx/cdx_bus.h +1 -0
@@ -115,6 +132,8 @@ struct cdx_driver {
 	int (*probe)(struct cdx_device *dev);
 	int (*remove)(struct cdx_device *dev);
 	void (*shutdown)(struct cdx_device *dev);
+	void (*reset_prepare)(struct cdx_device *dev);

This can never fail? Why not?

thanks,

greg k-h

author maintainer
#11 lore raw

Re: [PATCH v9 7/7] cdx: add device attributes

Quoted 48 lines · Greg KH

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 44 lines · Nipun Gupta

Create sysfs entry for CDX devices.

Sysfs entries provided in each of the CDX device detected by the CDX controller
- vendor id
- device id
- remove
- reset of the device.
- driver override

---
  Documentation/ABI/testing/sysfs-bus-cdx |  44 +++++++
  drivers/cdx/cdx.c                       | 145 ++++++++++++++++++++++++
  drivers/cdx/controller/cdx_controller.c |  19 ++++
  drivers/cdx/controller/mcdi_functions.c |  14 +++
  drivers/cdx/controller/mcdi_functions.h |  11 ++
  include/linux/cdx/cdx_bus.h             |  27 +++++
  6 files changed, 260 insertions(+)
Documentation/ABI/testing/sysfs-bus-cdx +12 -0
index 063d1a0dd866..c553ce3a449a 100644
@@ -10,3 +10,47 @@ Description:
               For example::
 
                 # echo 1 > /sys/bus/cdx/rescan
+
+What:                /sys/bus/cdx/devices/.../vendor
+Date:                March 2023
+Contact:     [email protected]
+Description:
+             Vendor ID for this CDX device. Vendor ID is 16 bit
+             identifier which is specific to the device manufacturer.
+             Combination of Vendor ID and Device ID identifies a device.
+
+What:                /sys/bus/cdx/devices/.../device
+Date:                March 2023
+Contact:     [email protected]
Quoted 8 lines

Documentation/ABI/testing/sysfs-bus-cdx Description: · line 29

Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-cdx +5 -0
+Description:
+             Device ID for this CDX device. Device ID is a 16 bit
+             identifier to identify a device type within the range
+             of a device manufacturer.
+             Combination of Vendor ID and Device ID identifies a device.

Are these printed out in hex, decimal, octal, binary, something else? Please be specific.

These will be printed in hex. Will update this as suggested.

Quoted 17 lines
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-cdx +3 -0
+
+What:                /sys/bus/cdx/devices/.../reset
+Date:                March 2023

Documentation/ABI/testing/sysfs-bus-cdx Description: · line 42

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-cdx +10 -0
+Contact:     [email protected]
+Description:
+             Writing a non-zero value to this file resets the CDX device.
+             On resetting the device, the corresponding driver is notified
+             twice, once before the device is being reset, and again after
+             the reset has been complete.
+
+             For example::
+
+               # echo 1 > /sys/bus/cdx/.../reset

Don't do "non-zero", make this explicit. This implies I can do "echo X" and it will work, do you want that?

We will update sysfs implementation to use kstrtobool and update the description accordingly.

Quoted 18 lines
Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-cdx +4 -0
+
+What:                /sys/bus/cdx/devices/.../remove
+Date:                March 2023
+Contact:     [email protected]

Documentation/ABI/testing/sysfs-bus-cdx Description: · line 56

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-cdx +10 -0
+Description:
+             Writing a non-zero value to this file removes the corresponding
+             device from the CDX bus. If the device is to be reconfigured
+             reconfigured in the Hardware, the device can be removed, so
+             that the device driver does not access the device while it is
+             being reconfigured.
+
+             For example::
+
+               # echo 1 > /sys/bus/cdx/devices/.../remove

Again, not non-zero.

Quoted 2 lines

Please use the built-in sysfs function to handle yes/no values being written to sysfs files for this and only accept the "yes" value.

Sure, will update accordingly.

Quoted 26 lines
Quoted 26 lines
drivers/cdx/cdx.c +17 -0
index c981232486dd..0c4f300373e5 100644
@@ -71,6 +71,39 @@
  /* CDX controllers registered with the CDX bus */
  static DEFINE_XARRAY_ALLOC(cdx_controllers);
 
+/**
+ * cdx_dev_reset - Reset a CDX device
+ * @dev: CDX device
+ *
+ * Return: -errno on failure, 0 on success.
+ */
+int cdx_dev_reset(struct device *dev)
+{
+     struct cdx_device *cdx_dev = to_cdx_device(dev);
+     struct cdx_controller *cdx = cdx_dev->cdx;
+     struct cdx_device_config dev_config;
+     struct cdx_driver *cdx_drv;
+     int ret;
+
+     cdx_drv = to_cdx_driver(dev->driver);
+     /* Notify driver that device is being reset */
+     if (cdx_drv && cdx_drv->reset_prepare)
Quoted 8 lines

drivers/cdx/cdx.c line 95

Quoted 5 lines
drivers/cdx/cdx.c +5 -0
+             cdx_drv->reset_prepare(cdx_dev);
+
+     dev_config.type = CDX_DEV_RESET_CONF;
+     ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
+                                   cdx_dev->dev_num, &dev_config);

You configure something off of the stack? Is that wise, it contains loads of invalid stack-provided data, how does this work at all?

For reset it is not using any data and checks only the flag, but yes it makes sense to memset to zero before passing to dev_configure.

Quoted 31 lines
Quoted 31 lines
drivers/cdx/cdx.c +23 -0
+     if (ret)
+             dev_err(dev, "cdx device reset failed\n");
+
+     /* Notify driver that device reset is complete */
+     if (cdx_drv && cdx_drv->reset_done)
+             cdx_drv->reset_done(cdx_dev);
+
+     return ret;
+}
+EXPORT_SYMBOL_GPL(cdx_dev_reset);
+
  /**
   * cdx_unregister_device - Unregister a CDX device
   * @dev: CDX device
@@ -237,6 +270,117 @@ static int cdx_dma_configure(struct device *dev)
       return 0;
  }
 
+/* show configuration fields */
+#define cdx_config_attr(field, format_string)        \
+static ssize_t       \
+field##_show(struct device *dev, struct device_attribute *attr, char *buf)   \
+{    \
+     struct cdx_device *cdx_dev = to_cdx_device(dev);        \
+     return sysfs_emit(buf, format_string, cdx_dev->field);  \
+}    \
+static DEVICE_ATTR_RO(field)
+
+cdx_config_attr(vendor, "0x%04x\n");
+cdx_config_attr(device, "0x%04x\n");
Quoted 8 lines

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev) · line 290

Quoted 6 lines
drivers/cdx/cdx.c +6 -0
+
+static ssize_t remove_store(struct device *dev,
+                         struct device_attribute *attr,
+                         const char *buf, size_t count)
+{
+     unsigned long val = 0;

Why initialize this?

Agree, will remove.

Quoted 7 lines

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev) · line 293

Quoted 4 lines
drivers/cdx/cdx.c +3 -0
+
+     if (kstrtoul(buf, 0, &val) < 0)
+             return -EINVAL;

As mentioned above, just use kstrtobool() instead, don't mess around with unsigned longs, that's not needed at all.

Sure, will use kstrtobool().

Quoted 17 lines
Quoted 4 lines
drivers/cdx/cdx.c +3 -0
+static ssize_t driver_override_store(struct device *dev,
+                                  struct device_attribute *attr,
+                                  const char *buf, size_t count)

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev) · line 342

Quoted 10 lines
drivers/cdx/cdx.c +10 -0
+{
+     struct cdx_device *cdx_dev = to_cdx_device(dev);
+     const char *old = cdx_dev->driver_override;
+     char *driver_override;
+     char *cp;
+
+     if (WARN_ON(dev->bus != &cdx_bus_type))
+             return -EINVAL;
+
+     if (count >= (PAGE_SIZE - 1))

And how can that happen?

Quoted 1 line

And why does the page size matter?

Looking at the latest code, there is driver_set_override() defined which can be directly called here. It internally does the check on count. I will update the code to use driver_set_override() API.

Quoted 40 lines
Quoted 26 lines
drivers/cdx/cdx.c +25 -0
+             return -EINVAL;
+
+     driver_override = kstrndup(buf, count, GFP_KERNEL);
+     if (!driver_override)
+             return -ENOMEM;
+
+     cp = strchr(driver_override, '\n');
+     if (cp)
+             *cp = '\0';
+
+     if (strlen(driver_override)) {
+             cdx_dev->driver_override = driver_override;
+     } else {
+             kfree(driver_override);
+             cdx_dev->driver_override = NULL;
+     }
+
+     kfree(old);
+
+     return count;
+}
+
+static int cdx_configure_device(struct cdx_controller *cdx,
+                             u8 bus_num, u8 dev_num,
+                             struct cdx_device_config *dev_config)

drivers/cdx/cdx.c static int cdx_dma_configure(struct device *dev)

Quoted 10 lines
drivers/cdx/cdx.c +10 -0
+{
+     int ret = 0;
+
+     switch (dev_config->type) {
+     case CDX_DEV_RESET_CONF:
+             ret = cdx_mcdi_reset_device(cdx->priv, bus_num, dev_num);
+             break;
+     default:
+             dev_err(cdx->dev, "Invalid device configuration flag\n");
+             ret = -EINVAL;

Can userspace cause this to happen? If so, please do not allow it to spam the kernel log.

Quoted 1 line

If not, who will see this error?

User-space cannot cause the default case to be hit, and yes you are correct this case will not be called from anywhere. I will remove the error print from the default case.

Quoted 105 lines
Quoted 105 lines
drivers/cdx/cdx.c +6 -0
+     }
+
+     return ret;
+}
+
  static int cdx_scan_devices(struct cdx_controller *cdx)
  {
       struct cdx_mcdi *cdx_mcdi = cdx->priv;
@@ -104,6 +122,7 @@ static int cdx_scan_devices(struct cdx_controller *cdx)
 
  static struct cdx_ops cdx_ops = {
       .scan           = cdx_scan_devices,
+     .dev_configure  = cdx_configure_device,
  };
 
  static int xlnx_cdx_probe(struct platform_device *pdev)
drivers/cdx/controller/mcdi_functions.c +14 -0
index 012b52881dd5..0158f26533dd 100644
@@ -123,3 +123,17 @@ int cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
 
       return 0;
  }
+
+int cdx_mcdi_reset_device(struct cdx_mcdi *cdx, u8 bus_num, u8 dev_num)
+{
+     MCDI_DECLARE_BUF(inbuf, MC_CMD_CDX_DEVICE_RESET_IN_LEN);
+     int ret;
+
+     MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_BUS, bus_num);
+     MCDI_SET_DWORD(inbuf, CDX_DEVICE_RESET_IN_DEVICE, dev_num);
+
+     ret = cdx_mcdi_rpc(cdx, MC_CMD_CDX_DEVICE_RESET, inbuf, sizeof(inbuf),
+                        NULL, 0, NULL);
+
+     return ret;
+}
drivers/cdx/controller/mcdi_functions.h +11 -0
index 6bf5a4a0778f..7440ace5539a 100644
@@ -47,4 +47,15 @@ int cdx_mcdi_get_dev_config(struct cdx_mcdi *cdx,
                           u8 bus_num, u8 dev_num,
                           struct cdx_dev_params *dev_params);
 
+/**
+ * cdx_mcdi_reset_device - Reset cdx device represented by bus_num:dev_num
+ * @cdx: pointer to MCDI interface.
+ * @bus_num: Bus number.
+ * @dev_num: Device number.
+ *
+ * Return: 0 on success, <0 on failure
+ */
+int cdx_mcdi_reset_device(struct cdx_mcdi *cdx,
+                       u8 bus_num, u8 dev_num);
+
  #endif /* CDX_MCDI_FUNCTIONS_H */
include/linux/cdx/cdx_bus.h +17 -0
index d134e0104724..35ef41d8a61a 100644
@@ -21,8 +21,20 @@
  /* Forward declaration for CDX controller */
  struct cdx_controller;
 
+enum {
+     CDX_DEV_RESET_CONF,
+};
+
+struct cdx_device_config {
+     u8 type;
+};
+
  typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
 
+typedef int (*cdx_dev_configure_cb)(struct cdx_controller *cdx,
+                                 u8 bus_num, u8 dev_num,
+                                 struct cdx_device_config *dev_config);
+
  /**
   * CDX_DEVICE_DRIVER_OVERRIDE - macro used to describe a CDX device with
   *                              override_only flags.
@@ -39,9 +51,12 @@ typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
  /**
   * struct cdx_ops - Callbacks supported by CDX controller.
   * @scan: scan the devices on the controller
+ * @dev_configure: configuration like reset, master_enable,
+ *              msi_config etc for a CDX device
   */
  struct cdx_ops {
       cdx_scan_cb scan;
+     cdx_dev_configure_cb dev_configure;
  };
 
  /**
@@ -101,6 +116,8 @@ struct cdx_device {
   * @probe: Function called when a device is added
   * @remove: Function called when a device is removed
   * @shutdown: Function called at shutdown time to quiesce the device
+ * @reset_prepare: Function called before is reset to notify driver
+ * @reset_done: Function called after reset is complete to notify driver
   * @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA.
   *           For most device drivers, no need to care about this flag
Quoted 8 lines

include/linux/cdx/cdx_bus.h struct cdx_driver { · line 135

Quoted 6 lines
include/linux/cdx/cdx_bus.h +1 -0
   *           as long as all DMAs are handled through the kernel DMA API.
@@ -115,6 +132,8 @@ struct cdx_driver {
       int (*probe)(struct cdx_device *dev);
       int (*remove)(struct cdx_device *dev);
       void (*shutdown)(struct cdx_device *dev);
+     void (*reset_prepare)(struct cdx_device *dev);

This can never fail? Why not?

It is just a notification to the device driver to prepare for the reset. Though it may fail, but the reset will still be triggered on the device irrespecive of the pass/fail status of reset_prepare . It has similar semantics to PCI.

Thanks,
Nipun

Quoted 4 lines

thanks,

greg k-h