Skip to contentSkip to conversation

i2c: Add drivers for the AMD PCIe MP2 I2C controller

PATCH Superseded Elie Morisse 2 participants 2 messages Dec 22, 2018 – Dec 26, 2018

Subsystems (3)

v14 of 17 Superseded Latest is v17 →

lore
Share & tools
lore

Elie Morisse

author maintainer
#0 lore raw

PATCH v14 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 MMIO 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

Changes since v13 by Elie M., requested by Nehal Shah from AMD:

-> split the i2c-amd-mp2 module back into two, i2c-amd-mp2-pci and i2c-amd-mp2-plat
-> move DMA mapping back into the platform driver

 Documentation/i2c/busses/i2c-amd-mp2  |  24 +
 MAINTAINERS                           |   8 +
 drivers/i2c/busses/Kconfig            |  21 +
 drivers/i2c/busses/Makefile           |   2 +
 drivers/i2c/busses/i2c-amd-mp2-pci.c  | 650 ++++++++++++++++++++++++++
 drivers/i2c/busses/i2c-amd-mp2-plat.c | 390 ++++++++++++++++
 drivers/i2c/busses/i2c-amd-mp2.h      | 223 +++++++++
 7 files changed, 1318 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 +21 -0
index f2c681971201..ef7617dd5d01 100644
@@ -77,6 +77,27 @@ 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"
+	depends on PCI
+	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-pci.
+
+config I2C_AMD_MP2_PLATFORM
+	tristate "AMD MP2 Platform"
+	select I2C_AMD_MP2_PCI
+	depends on ACPI
+	help
+	  If you say yes to this option, support will be included for the AMD
+	  MP2 I2C adapter.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called i2c-amd-mp2-plat.
+
 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..4fa829dccf68 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-pci.o
+obj-$(CONFIG_I2C_AMD_MP2_PLATFORM)	+= 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 +650 -0

650 lines not on this page · raw message

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

390 lines not on this page · raw message

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

223 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw
I love your patch! Yet something to improve:

Hi Elie,

I love your patch! Yet something to improve:

[auto build test ERROR on wsa/i2c/for-next]
[also build test ERROR on v4.20 next-20181224]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Elie-Morisse/i2c-Add-drivers-for-the-AMD-PCIe-MP2-I2C-controller/20181223-034458
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next
config: i386-randconfig-x013-12261115 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

drivers/i2c/busses/i2c-amd-mp2-plat.c: In function 'i2c_amd_probe':

>> drivers/i2c/busses/i2c-amd-mp2-plat.c:294:17: error: 'struct amd_i2c_common' has no member named 'suspend'
     i2c_dev->common.suspend = &i2c_amd_suspend;
                    ^
>> drivers/i2c/busses/i2c-amd-mp2-plat.c:294:29: error: 'i2c_amd_suspend' undeclared (first use in this function); did you mean 'i2c_amd_func'?
     i2c_dev->common.suspend = &i2c_amd_suspend;
                                ^~~~~~~~~~~~~~~
                                i2c_amd_func
   drivers/i2c/busses/i2c-amd-mp2-plat.c:294:29: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/i2c/busses/i2c-amd-mp2-plat.c:295:18: error: 'struct amd_i2c_common' has no member named 'resume'; did you mean 'reqcmd'?
     i2c_dev->common.resume = &i2c_amd_resume;
                     ^~~~~~
                     reqcmd
>> drivers/i2c/busses/i2c-amd-mp2-plat.c:295:28: error: 'i2c_amd_resume' undeclared (first use in this function); did you mean 'i2c_amd_probe'?
     i2c_dev->common.resume = &i2c_amd_resume;
                               ^~~~~~~~~~~~~~
                               i2c_amd_probe

vim +294 drivers/i2c/busses/i2c-amd-mp2-plat.c

263
264 static int i2c_amd_probe(struct platform_device *pdev)
265 {
266 int ret;
267 struct amd_i2c_dev *i2c_dev;
268 acpi_handle handle = ACPI_HANDLE(&pdev->dev);
269 struct acpi_device *adev;
270 struct amd_mp2_dev *mp2_dev;
271 const char *uid;
272
273 if (acpi_bus_get_device(handle, &adev))
274 return -ENODEV;
275
276 /* The ACPI namespace doesn't contain information about which MP2 PCI
277 * device an AMDI0011 ACPI device is related to, so assume that there's
278 * only one MP2 PCI device per system.
279 */
280 mp2_dev = amd_mp2_find_device();
281 if (!mp2_dev)
282 /* The MP2 PCI device might get probed later */
283 return -EPROBE_DEFER;
284
285 i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL);
286 if (!i2c_dev)
287 return -ENOMEM;
288
289 i2c_dev->common.mp2_dev = mp2_dev;
290 i2c_dev->pdev = pdev;
291 platform_set_drvdata(pdev, i2c_dev);
292
293 i2c_dev->common.cmd_completion = &i2c_amd_cmd_completion;

 > 294		i2c_dev->common.suspend = &i2c_amd_suspend;
 > 295		i2c_dev->common.resume = &i2c_amd_resume;

296
297 uid = adev->pnp.unique_id;
298 if (!uid) {
299 dev_err(&pdev->dev, "missing UID/bus id!\n");
300 return -EINVAL;
301 } else if (strcmp(uid, "0") == 0) {
302 i2c_dev->common.bus_id = 0;
303 } else if (strcmp(uid, "1") == 0) {
304 i2c_dev->common.bus_id = 1;
305 } else {
306 dev_err(&pdev->dev, "incorrect UID/bus id \"%s\"!\n", uid);
307 return -EINVAL;
308 }
309 dev_dbg(&pdev->dev, "bus id is %u\n", i2c_dev->common.bus_id);
310
311 /* Register the adapter */
312 amd_mp2_pm_runtime_get(mp2_dev);
313
314 i2c_dev->common.reqcmd = i2c_none;
315 if (amd_mp2_register_cb(&i2c_dev->common))
316 return -EINVAL;
317 device_link_add(&i2c_dev->pdev->dev, &mp2_dev->pci_dev->dev,
318 DL_FLAG_AUTOREMOVE_CONSUMER);
319
320 i2c_dev->common.i2c_speed = i2c_amd_get_bus_speed(pdev);
321
322 /* Setup i2c adapter description */
323 i2c_dev->adap.owner = THIS_MODULE;
324 i2c_dev->adap.algo = &i2c_amd_algorithm;
325 i2c_dev->adap.quirks = &amd_i2c_dev_quirks;
326 i2c_dev->adap.dev.parent = &pdev->dev;
327 i2c_dev->adap.algo_data = i2c_dev;
328 i2c_dev->adap.nr = pdev->id;
329 i2c_dev->adap.timeout = AMD_I2C_TIMEOUT;
330 ACPI_COMPANION_SET(&i2c_dev->adap.dev, ACPI_COMPANION(&pdev->dev));
331 i2c_dev->adap.dev.of_node = pdev->dev.of_node;
332 snprintf(i2c_dev->adap.name, sizeof(i2c_dev->adap.name),
333 "AMD MP2 i2c bus %u", i2c_dev->common.bus_id);
334 i2c_set_adapdata(&i2c_dev->adap, i2c_dev);
335
336 init_completion(&i2c_dev->cmd_complete);
337
338 /* Enable the bus */
339 if (i2c_amd_enable_set(i2c_dev, true))
340 dev_err(&pdev->dev, "initial bus enable failed\n");
341
342 /* Attach to the i2c layer */
343 ret = i2c_add_numbered_adapter(&i2c_dev->adap);
344
345 amd_mp2_pm_runtime_put(mp2_dev);
346
347 if (ret < 0)
348 dev_err(&pdev->dev, "i2c add adapter failed = %d\n", ret);
349
350 return ret;
351 }
352

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation