Skip to contentSkip to conversation

i2c: Add drivers for the AMD PCIe MP2 I2C controller

PATCH Superseded Elie Morisse 2 participants 3 messages Dec 9, 2018 – Dec 14, 2018

Subsystems (3)

v13 of 17 Superseded Latest is v17 →

lore
Share & tools
lore

Elie Morisse

author maintainer
#0 lore raw

PATCH v13 1/1 i2c: Add drivers for the AMD PCIe MP2 I2C controller

MP2 controllers have two separate busses, so may accommodate up to two I2C adapters. Those adapters are listed in the ACPI namespace with the "AMDI0011" HID, and probed by a platform driver.

Communication with the MP2 takes place through iomapped registers, or through DMA for more than 32 bytes transfers.

This is major rework of the patch submitted by Nehal-bakulchandra Shah from AMD (https://patchwork.kernel.org/patch/10597369/).

Most of the event handling of v3 was rewritten to make it work with more than one bus (e.g on Ryzen-based Lenovo Yoga 530), and this version contains many other improvements.

---
Changes since v1 (https://www.spinics.net/lists/linux-i2c/msg34650.html):
-> Add fix for IOMMU
-> Add dependency of ACPI
-> Add locks to avoid the crash

Changes since v2 (https://patchwork.ozlabs.org/patch/961270/):

-> fix for review comments
-> fix for more than 32 bytes write issue

Changes since v3 (https://patchwork.kernel.org/patch/10597369/) by Elie M.:

-> support more than one bus/adapter
-> support more than one slave per bus
-> use the bus speed specified by the slaves declared in the DSDT instead of
assuming speed == 400kbits/s
-> instead of kzalloc'ing a buffer for every less than 32 bytes reads, simply
use i2c_msg.buf
-> fix buffer overreads/overflows when (<=32 bytes) message lengths aren't a
multiple of 4 by using memcpy_fromio and memcpy_toio
-> use streaming DMA mappings instead of allocating a coherent DMA buffer for
every >32 bytes read/write
-> properly check for timeouts during i2c_amd_xfer and increase it from 50
jiffies to 250 msecs (which is more in line with other drivers)
-> complete amd_i2c_dev.msg even if the device doesn't return a xxx_success
event, instead of stalling i2c_amd_xfer
-> removed the spinlock and mdelay during i2c_amd_pci_configure, I didn't see
the point since it's already waiting for a i2c_busenable_complete event
-> add an adapter-specific mutex lock for i2c_amd_xfer, since we don't want
parallel calls writing to AMD_C2P_MSG0 (or AMD_C2P_MSG1)
-> add a global mutex lock for registers AMD_C2P_MSG2 to AMD_C2P_MSG9, which
are shared across the two busses/adapters
-> add MODULE_DEVICE_TABLE to automatically load i2c-amd-platdrv if the DSDT
enumerates devices with the "AMDI0011" HID
-> set maximum length of reads/writes to 4095 (event's length field is 12 bits) -> basic PM support
-> style corrections to match the kernel code style, and tried to reduce code
duplication whenever possible

Changes since v4 by Elie M.:

-> fix missing typecast warning
-> removed the duplicated entry in Kconfig

Changes since v5 by Elie M.:

-> move DMA mapping from the platform driver to the PCI driver
-> attempt to find the platform device's PCI parent through the _DEP ACPI method
(if not found take the first MP2 device registred in the i2c-amd-pci-mp2 driver, like before)
-> do not assume anymore that the PCI device is owned by the i2c-amd-pci-mp2
driver
-> address other review comments by Bjorn Helgaas (meant for v3)

Changes since v6 by Elie M.:

-> remove unnecessary memcpy from the DMA buffer during i2c_amd_read_completion

Changes since v7 by Elie M.:

-> merge the two modules into one named i2c-amd-mp2, delete now useless exports -> unlock the C2P mutex if i2c_amd_xfer_msg timeouts, to prevent stalling the
MP2 controller if a slave doesn't respond to a read or write request
-> unmap the DMA buffer before read/write_complete
-> fix bus_disable commands handling (no more errors during module exit) -> prefer managed versions of pci_ and dev_ functions whenever possible -> increase adapter retries from 0 to 3
-> reduce code duplication in debugfs functions
-> address other review points by Bjorn Helgaas (except regarding the _DEP
hint)

Changes since v8 by Elie M.:

-> remove mostly redundant amd_i2c_rw_config, refer to i2c_msg whenever possible
-> use i2c_get_dma_safe_msg_buf and i2c_put_dma_safe_msg_buf -> defer probe() by the platform driver if no MP2 device has been probed yet by the PCI driver (which should address Bjorn Helgaas' concern while preserving the platform driver)
-> if the interrupt following a command doesn't get handled after 250ms, zero AMD_P2C_MSG_INTEN to prevent the MP2 from stalling for a few more seconds
-> include AMD_P2C_MSG3 and fix typo in debugfs output
-> cosmetic fixes, code reduction, and better comments
-> add Nehal Shah and Shyam Sundar S K from AMD to the list of maintainers

Changes since v9 by Elie M.:

-> remove the xfer_lock mutex which was redundant with i2c_adapter.bus_lock -> platform device remove() fixes (protection against the very unlikely case that both the PCI and platform devices get detached manually and simultaneously)
-> fix manually unbinding the PCI device and then rebinding, there was an interrupt that wouldn't get serviced and thus the line got disabled -> look for the MP2 device corresponding to an AMDI0011 ACPI device in every device enumerated by _DEP, not just the first one
-> set i2c_adapter.nr to pdev->id and call i2c_add_numbered_adapter() -> add Documentation/i2c/busses/ entry
-> minor enhancements

Changes since v10 by Elie M.:

-> runtime PM support
-> synchronisation fixes for <=32 bytes reads/writes through C2P registers, no more timeouts or "irq: nobody cared" errors on Lenovo Ideapad 530s -> remove the isr spinlock, not needed since isr aren't re-entrant -> remove the delayed_work field, make the isr complete .cmd_complete directly
-> xfer now returns -EIO if the MP2 response isn't the expected one -> check that the adapter still exists at the beginning of xfer -> less code duplication in amd_mp2_read/write
-> better names (bus_xnable => bus_enable_set, _msg => _cmd) and other minor enhancements

Changes since v11 by Elie M.:

-> remove the ACPI _DEP hint, Nehal from AMD said that there's no plan to have more than one MP2 device per system and this wasn't _DEP's purpose -> use device links between the two drivers to simplify the .remove logic -> do not hardcode the i2c adapter timeout
-> code reduction and comment fixes

Changes since v12 by Elie M.:

-> fix the two warnings by the kbuild test bot

 Documentation/i2c/busses/i2c-amd-mp2  |  24 +
 MAINTAINERS                           |   8 +
 drivers/i2c/busses/Kconfig            |  10 +
 drivers/i2c/busses/Makefile           |   2 +
 drivers/i2c/busses/i2c-amd-mp2-pci.c  | 689 ++++++++++++++++++++++++++
 drivers/i2c/busses/i2c-amd-mp2-plat.c | 342 +++++++++++++
 drivers/i2c/busses/i2c-amd-mp2.h      | 228 +++++++++
 7 files changed, 1303 insertions(+)
 create mode 100644 Documentation/i2c/busses/i2c-amd-mp2
 create mode 100644 drivers/i2c/busses/i2c-amd-mp2-pci.c
 create mode 100644 drivers/i2c/busses/i2c-amd-mp2-plat.c
 create mode 100644 drivers/i2c/busses/i2c-amd-mp2.h
Documentation/i2c/busses/i2c-amd-mp2 new file +24 -0
new file mode 100644
index 000000000000..4b3d4b804413
@@ -0,0 +1,24 @@
+Kernel driver i2c-amd-mp2
+
+Supported adapters:
+  * AMD MP2 PCIe interface
+
+Datasheet: not publicly available.
+
+Authors:
+	Shyam Sundar S K <[email protected]>
+	Nehal Shah <[email protected]>
+	Elie Morisse <[email protected]>
+
+Description
+-----------
+
+The MP2 is an ARM processor programmed as an I2C controller and communicating
+with the x86 host through PCI.
+
+If you see something like this:
+
+03:00.7 Non-VGA unclassified device: Advanced Micro Devices, Inc. [AMD] Device
+									15e6
+
+in your 'lspci -v', then this driver is for your device.
MAINTAINERS +8 -0
index d27401df091f..76f8b3c40132 100644
@@ -808,6 +808,14 @@ F:	drivers/gpu/drm/amd/include/vi_structs.h
 F:	drivers/gpu/drm/amd/include/v9_structs.h
 F:	include/uapi/linux/kfd_ioctl.h
 
+AMD MP2 I2C DRIVER
+M:	Elie Morisse <[email protected]>
+M:	Nehal Shah <[email protected]>
+M:	Shyam Sundar S K <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/i2c/busses/i2c-amd-mp2*
+
 AMD POWERPLAY
 M:	Rex Zhu <[email protected]>
 M:	Evan Quan <[email protected]>
drivers/i2c/busses/Kconfig +10 -0
index f2c681971201..d38b83d43088 100644
@@ -77,6 +77,16 @@ config I2C_AMD8111
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-amd8111.
 
+config I2C_AMD_MP2_PCI
+	tristate "AMD MP2 PCIe I2C adapter"
+	depends on PCI && ACPI
+	help
+	  If you say yes to this option, support will be included for the AMD
+	  MP2 PCIe I2C adapter.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called i2c-amd-mp2.
+
 config I2C_HIX5HD2
 	tristate "Hix5hd2 high-speed I2C driver"
 	depends on ARCH_HISI || ARCH_HIX5HD2 || COMPILE_TEST
drivers/i2c/busses/Makefile +2 -0
index 5f0cb6915969..fc5a19755684 100644
@@ -33,6 +33,8 @@ obj-$(CONFIG_I2C_POWERMAC)	+= i2c-powermac.o
 
 # Embedded system I2C/SMBus host controller drivers
 obj-$(CONFIG_I2C_ALTERA)	+= i2c-altera.o
+obj-$(CONFIG_I2C_AMD_MP2_PCI)	+= i2c-amd-mp2.o
+i2c-amd-mp2-objs := i2c-amd-mp2-pci.o i2c-amd-mp2-plat.o
 obj-$(CONFIG_I2C_ASPEED)	+= i2c-aspeed.o
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
drivers/i2c/busses/i2c-amd-mp2-pci.c new file +689 -0

689 lines not on this page · raw message

drivers/i2c/busses/i2c-amd-mp2-plat.c new file +342 -0

342 lines not on this page · raw message

drivers/i2c/busses/i2c-amd-mp2.h new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw
Quoted 7 lines · Elie Morisse

MP2 controllers have two separate busses, so may accommodate up to two I2C adapters. Those adapters are listed in the ACPI namespace with the "AMDI0011" HID, and probed by a platform driver.

Communication with the MP2 takes place through iomapped registers, or through DMA for more than 32 bytes transfers.

Quoted 8 lines

This is major rework of the patch submitted by Nehal-bakulchandra Shah from AMD (https://patchwork.kernel.org/patch/10597369/).

Most of the event handling of v3 was rewritten to make it work with more than one bus (e.g on Ryzen-based Lenovo Yoga 530), and this version contains many other improvements.

Nehal Shah, are you happy with this driver from your side?

author maintainer
#2 lore raw

Hi,

Having the patch reviewed by Nehal Shah would be great. While waiting to hear from him, he voiced some concerns in the v10 thread:
https://lkml.org/lkml/2018/11/27/62
The _DEP hint was removed in v12. Regarding AMD's intention of having two drivers, there are still two drivers, just within the same module. And the comment about where the DMA mapping code should be is minor and it's trivial to move it back if AMD plans to expand the platform driver.

Noone reported any issue since v11, I'm done improving the drivers unless it's necessary in order to get it accepted upstream. Please let me know if I still have to change something to get it merged.

Elie Morisse

Quoted 20 lines · Wolfram Sang
Quoted 7 lines · Elie Morisse

MP2 controllers have two separate busses, so may accommodate up to two I2C adapters. Those adapters are listed in the ACPI namespace with the "AMDI0011" HID, and probed by a platform driver.

Communication with the MP2 takes place through iomapped registers, or through DMA for more than 32 bytes transfers.

Quoted 8 lines

This is major rework of the patch submitted by Nehal-bakulchandra Shah from AMD (https://patchwork.kernel.org/patch/10597369/).

Most of the event handling of v3 was rewritten to make it work with more than one bus (e.g on Ryzen-based Lenovo Yoga 530), and this version contains many other improvements.

Nehal Shah, are you happy with this driver from your side?