Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp703398imu; Fri, 4 Jan 2019 05:43:36 -0800 (PST) X-Google-Smtp-Source: AFSGD/WD4MxUJp7iQVzu9Hk2f+ExfFwGTXwo7TX85Gbx/IbA3n89UTxRA2hc9aLPEouWQSeO053u X-Received: by 2002:a62:3006:: with SMTP id w6mr52852244pfw.258.1546609416473; Fri, 04 Jan 2019 05:43:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546609416; cv=none; d=google.com; s=arc-20160816; b=sI6A4RcyZzlIv3z1TWxK+EQqZsIwC4zTz2T5BtwAsKmWgxTBvbMAfFE4xp0+yktFfO qzGWz0ohEaxAWc4udZBL9zEkIDzTmgWWB7+DSq1IgO60mKVj0bWdT/OfR0akhLkKYboi KSyDAJ0vfG6hguFvboRITkOFnoMqjH3z9BEESUWDYJnJzfq2iP1oQp5CvzE+teDr3h/E 4CYA9n6cDqiSlRc7fJ5Dre3JkgqK96gb8PrJViOgSmmPlxoBSw7p0sPHSWhzVvWHIBNI +VpxPwZrn2m0KnF/w9a/F1I2Lr5E0I0HNJEJqoIpErVaEhxWYY2R7tTJhI3KRDGWuxyl QIag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=LgxiyfPxIm4dphnlH0vD3ys5r3okO8pr81wAJcAkY7I=; b=uAJS2QC4s8jGrijsxBN2Al+jnbIBOIRTqO2yvD7Pdg4Ips4ozqSg6nq8+hlxxem5Wi tSPKsK1WNib36w5VyPJrAWYXZLX0ReKdhD8EEE/jEpNF0kY87r6gW+kv1S73C0jdSTqH oWEfRPtNN4pBR17Dghf0lNrn5MElTXoKcEy1ydE18pfMcMWXbHq5R2Owbvn/H6qEqnHc e9XDudnEtcPisPqDL+q+1kWwZbs58CO/r8FUWW+ZzsvAhvHYMzzTZPjy5QPf8oPhj+Ki Q8Ps+apSOjJsAA7/Fe89F2y0VLOmvmTk5oaHO8x9rZqkMARGu2puemMyV4SlZQ6i6HeC skNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jaQHjWZj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p15si53014897plq.24.2019.01.04.05.43.19; Fri, 04 Jan 2019 05:43:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jaQHjWZj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727133AbfADLjF (ORCPT + 99 others); Fri, 4 Jan 2019 06:39:05 -0500 Received: from mail-qt1-f196.google.com ([209.85.160.196]:33271 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726462AbfADLjE (ORCPT ); Fri, 4 Jan 2019 06:39:04 -0500 Received: by mail-qt1-f196.google.com with SMTP id l11so40076055qtp.0 for ; Fri, 04 Jan 2019 03:39:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=LgxiyfPxIm4dphnlH0vD3ys5r3okO8pr81wAJcAkY7I=; b=jaQHjWZj8UDvrG0uj3PpaKUC1z/o5WJSH2WgIHIqhAfsA+xQqjOigXmyAfgUZcsjSb mCaXJEJthlNVF1CLHJBcArKd8WkzAY7MKUrpHpls1mnO3/5F7zPA/apiD05nhPldYoMI 4Mw/8Ndc7AyQa4CEY9dat1EHmLXcTvtez0nPncbbDNjNS6t+hcpfiUVIzKBCYOKMkw7D h9KhRuTBGxtEjDF5uTU/OC/gDB9prU0xiHDWHiGueOqbeh8sKhVn1fmgBpIY5PkYchqT vAzfEjd+CIUm//Nru2AO5mFDg6N5eVZV16N+3DAIM2lhP9fGOVz86KUsqTXzk++KMiH+ 67nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=LgxiyfPxIm4dphnlH0vD3ys5r3okO8pr81wAJcAkY7I=; b=NdEmX/9kRa1Hi7KNA4TCr9O4dv8gTy8Yl0qaSAMJT8mgnfIUnfAMT8aX/8o9d+PDP2 rWZ/Cn6t1jMCQ5Wi892XBmEGO3dCOIhphopRri6Iub/PPWsuADhayiwHMDyNhW3QAd+l iUXqEwvQyt/OczLoOxkKrlM3BwSePQNW/xCgS6TEjtE25/Rrm+tR61Ww2vQXW5pQD/Wc u4I2zyNcdK1YaDDjaIk+wPD3LONsPGWg7tsy7TzG7/Qne+jO9Hp0iQjr53XysGTnBW0I v5V8Hr2xJY2FofDzglCM5dcAhmrSTQFMeGXVgmLc+wBerePEI9QxQBpd5QCfTsbfn28C SbuA== X-Gm-Message-State: AJcUukfE66FN/rESHz1zYMKKwmOsZgdEPlWNJDAW6xsS/EIYx/eCgaJB w5nHetTzGiVw6IvAOZENHYFOBeF2+0Gh2iqZjls= X-Received: by 2002:a0c:d933:: with SMTP id p48mr51539086qvj.15.1546601941950; Fri, 04 Jan 2019 03:39:01 -0800 (PST) MIME-Version: 1.0 References: <20181226075330.82462-1-pihsun@chromium.org> <20181226075330.82462-6-pihsun@chromium.org> In-Reply-To: From: Enric Balletbo Serra Date: Fri, 4 Jan 2019 12:38:50 +0100 Message-ID: Subject: Re: [RFC,5/5] mfd: cros_ec: add EC host command support using rpmsg. To: Peter Shih Cc: Nicolas Boichat , Lee Jones , Benson Leung , Olof Johansson , open list , Guenter Roeck Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Peter, Missatge de Peter Shih del dia dv., 4 de gen. 2019 a les 8:58: > > Thanks for the review. > I would leave some formatting comment to v2, and reply others first. > > On Fri, Jan 4, 2019 at 12:05 AM Enric Balletbo Serra > wrote: > > > > Hi, > > > > Many thanks for sending this. Please, add Guenter and me for next > > versions, we are interested in it, thanks :) > > > > Missatge de Pi-Hsun Shih del dia dc., 26 de des. > > 2018 a les 8:57: > > > > > > Add EC host command support through rpmsg. > > > > > > Signed-off-by: Pi-Hsun Shih > > > --- > > > drivers/mfd/cros_ec_dev.c | 9 ++ > > > drivers/platform/chrome/Kconfig | 8 ++ > > > drivers/platform/chrome/Makefile | 1 + > > > drivers/platform/chrome/cros_ec_rpmsg.c | 164 ++++++++++++++++++++++++ > > > include/linux/mfd/cros_ec.h | 1 + > > > include/linux/mfd/cros_ec_commands.h | 2 + > > > 6 files changed, 185 insertions(+) > > > create mode 100644 drivers/platform/chrome/cros_ec_rpmsg.c > > > > > > diff --git a/drivers/mfd/cros_ec_dev.c b/drivers/mfd/cros_ec_dev.c > > > index 2d0fee488c5aa8..67983853413d07 100644 > > > --- a/drivers/mfd/cros_ec_dev.c > > > +++ b/drivers/mfd/cros_ec_dev.c > > > @@ -414,6 +414,15 @@ static int ec_device_probe(struct platform_device *pdev) > > > device_initialize(&ec->class_dev); > > > cdev_init(&ec->cdev, &fops); > > > > > > + if (cros_ec_check_features(ec, EC_FEATURE_SCP)) { > > > + dev_info(dev, "SCP detected.\n"); > > > + /* > > > + * Help userspace differentiating ECs from SCP, > > > + * regardless of the probing order. > > > + */ > > > + ec_platform->ec_name = CROS_EC_DEV_SCP_NAME; > > > + } > > > + > > > > Why userspace should know that this is an SCP? From the userspace > > point of view shouldn't be this transparent, we don't do distinctions > > when the transport layer is i2c, spi or lpc, and I think that the > > cros_ec_rpmsg driver is a cros-ec transport layer, like these. So, I > > think that this is not needed. > > > > Since both the EC and the SCP talk in EC host command format here, and they can > both exist on the same system, if we don't do the distinction, both of them > would be registered as /dev/cros_ec, and cause an error. > Interesting, so this system will have two cros-ec, one connected via spi or i2c to the soc and another one using the M4 within the M8183? Actually, on some systems, we have chained EC's (ie cros_ec and cros_pd). The way we actually handle the name to access the different ECs is create a mfd cell with their specific platform data, I am wondering if we can do the same here (see drivers/mfd/cros_ec.c) > This change is actually independent to the rpmsg change (EC through all > transport layer can report that they have feature EC_FEATURE_SCP, and would > then be seen from userspace as /dev/cros_scp), I'll move this to another patch > in v2. > > > > /* > > > * Add the class device > > > * Link to the character device for creating the /dev entry > > > diff --git a/drivers/platform/chrome/Kconfig b/drivers/platform/chrome/Kconfig > > > index 16b1615958aa2d..b03d68eb732177 100644 > > > --- a/drivers/platform/chrome/Kconfig > > > +++ b/drivers/platform/chrome/Kconfig > > > @@ -72,6 +72,14 @@ config CROS_EC_SPI > > > response time cannot be guaranteed, we support ignoring > > > 'pre-amble' bytes before the response actually starts. > > > > > > +config CROS_EC_RPMSG > > > + tristate "ChromeOS Embedded Controller (rpmsg)" > > > + depends on MFD_CROS_EC && RPMSG > > > > I think that this driver is DT-only, && OF ? > > Would add this in v2. > > > > > > + help > > > + If you say Y here, you get support for talking to the ChromeOS EC > > > + through rpmsg. This uses a simple byte-level protocol with a > > > + checksum. > > > + > > > config CROS_EC_LPC > > > tristate "ChromeOS Embedded Controller (LPC)" > > > depends on MFD_CROS_EC && ACPI && (X86 || COMPILE_TEST) > > > diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Makefile > > > index cd591bf872bbe9..3e3190af2b50f4 100644 > > > --- a/drivers/platform/chrome/Makefile > > > +++ b/drivers/platform/chrome/Makefile > > > @@ -8,6 +8,7 @@ cros_ec_ctl-objs := cros_ec_sysfs.o cros_ec_lightbar.o \ > > > obj-$(CONFIG_CROS_EC_CTL) += cros_ec_ctl.o > > > obj-$(CONFIG_CROS_EC_I2C) += cros_ec_i2c.o > > > obj-$(CONFIG_CROS_EC_SPI) += cros_ec_spi.o > > > +obj-$(CONFIG_CROS_EC_RPMSG) += cros_ec_rpmsg.o > > > cros_ec_lpcs-objs := cros_ec_lpc.o cros_ec_lpc_reg.o > > > cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC) += cros_ec_lpc_mec.o > > > obj-$(CONFIG_CROS_EC_LPC) += cros_ec_lpcs.o > > > diff --git a/drivers/platform/chrome/cros_ec_rpmsg.c b/drivers/platform/chrome/cros_ec_rpmsg.c > > > new file mode 100644 > > > index 00000000000000..f123ca6d1c029c > > > --- /dev/null > > > +++ b/drivers/platform/chrome/cros_ec_rpmsg.c > > > @@ -0,0 +1,164 @@ > > > +// SPDX-License-Identifier: GPL-2.0 > > > +// > > > +// Copyright 2018 Google LLC. > > > + > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > + > > > +/** > > > + * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply > > > + * > > > + * This is only used for old EC proto version, and is not supported for this > > > + * driver. > > > + * > > > + * @ec_dev: ChromeOS EC device > > > + * @ec_msg: Message to transfer > > > + */ > > > +static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev, > > > + struct cros_ec_command *ec_msg) > > > +{ > > > + return -EINVAL; > > > +} > > > + > > > +/** > > > + * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply > > > + * > > > + * @ec_dev: ChromeOS EC device > > > + * @ec_msg: Message to transfer > > > + */ > > > +static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev, > > > + struct cros_ec_command *ec_msg) > > > +{ > > > + struct ec_host_response *response; > > > + struct rpmsg_device *rpdev = ec_dev->priv; > > > + int len; > > > + u8 sum; > > > + int ret; > > > + int i; > > > + > > > + ec_msg->result = 0; > > > + len = cros_ec_prepare_tx(ec_dev, ec_msg); > > > + dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); > > > + > > > + // TODO: This currently relies on that mtk_rpmsg send actually blocks > > > + // until ack. Should do the wait here instead. > > > > Use standard C style comments. > > > > > + ret = rpmsg_send(rpdev->ept, ec_dev->dout, len); > > > + > > > > Remove that empty line. > > > > > + if (ret) { > > > + dev_err(ec_dev->dev, "rpmsg send failed\n"); > > > + return ret; > > > + } > > > + > > > + /* check response error code */ > > > + response = (struct ec_host_response *)ec_dev->din; > > > + ec_msg->result = response->result; > > > + > > > + ret = cros_ec_check_result(ec_dev, ec_msg); > > > + if (ret) > > > + goto exit; > > > + > > > + if (response->data_len > ec_msg->insize) { > > > + dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", > > > + response->data_len, ec_msg->insize); > > > + ret = -EMSGSIZE; > > > + goto exit; > > > + } > > > + > > > + /* copy response packet payload and compute checksum */ > > > + memcpy(ec_msg->data, ec_dev->din + sizeof(*response), > > > + response->data_len); > > > + > > > + sum = 0; > > > + for (i = 0; i < sizeof(*response) + response->data_len; i++) > > > + sum += ec_dev->din[i]; > > > + > > > + if (sum) { > > > + dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n", > > > + sum); > > > + ret = -EBADMSG; > > > + goto exit; > > > + } > > > + > > > + ret = response->data_len; > > > +exit: > > > + if (ec_msg->command == EC_CMD_REBOOT_EC) > > > + msleep(EC_REBOOT_DELAY_MS); > > > > Can you explain why this sleep is needed? > > From the comment of EC_CMD_REBOOT_EC: "The EC is unresponsive for a time after > a reboot command. Add a simple delay to make sure that the bus stays locked." > > This is copied from other transport layer drivers, and probably not needed > since we would reload the firmware for SCP while it's rebooting. I would test > to see if this is needed when the reboot flow for SCP work as expected. > (There's still some firmware work need to be done before it can be tested...) > > > > > > + > > > + return ret; > > > +} > > > + > > > +static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data, > > > + int len, void *priv, u32 src) > > > +{ > > > + struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); > > > + > > > + if (len > ec_dev->din_size) { > > > + dev_warn(ec_dev->dev, > > > + "ipi received length %d > din_size, truncating", len); > > > + len = ec_dev->din_size; > > > + } > > > + > > > + memcpy(ec_dev->din, data, len); > > > + > > > + return 0; > > > +} > > > + > > > +static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev) > > > +{ > > > + struct device *dev = &rpdev->dev; > > > + > > Remove that empty line > > > > > + struct cros_ec_device *ec_dev; > > > + int ret; > > > + > > > + ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); > > > + if (!ec_dev) > > > + return -ENOMEM; > > > + > > > + ec_dev->dev = dev; > > > + ec_dev->priv = rpdev; > > > + ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg; > > > + ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg; > > > + ec_dev->phys_name = dev_name(&rpdev->dev); > > > + ec_dev->din_size = sizeof(struct ec_host_response) + > > > + sizeof(struct ec_response_get_protocol_info); > > > + ec_dev->dout_size = sizeof(struct ec_host_request); > > > + dev_set_drvdata(dev, ec_dev); > > > + > > > + ret = cros_ec_register(ec_dev); > > > + if (ret) > > > > I'd add an error message here > > > > dev_err(dev, "cannot register EC\n" > > > > > + return ret; > > > + > > > + return 0; > > > +} > > > + > > > +static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev) > > > > This function will not be needed after apply [1]. I would recommend > > base your patches on top of [2] > > > > [1] https://lkml.org/lkml/2018/12/12/672 > > [2] https://lkml.org/lkml/2018/12/12/679 > > Noted. > > > > > > +{ > > > + struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); > > > + > > > + cros_ec_remove(ec_dev); > > > +} > > > + > > > > How this driver is instantiated? > > > > I expect something like this here (like the other transport layers) > > > > static const struct of_device_id cros_ec_rpmsg_of_match[] = { > > { .compatible = "google,cros-ec-rpmsg", }, > > { } > > }; > > MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match); > > > > And the DT containing the compatible = "google,cros-ec-rpmsg" like the > > other cros-ec transport layers. > > This is a part that I'm getting quite confused on how to do properly. > For SPI, a spi_device is created for each node listed under spi node in device > tree. > spi0 { > compatible = "xxx-spi"; > cros_ec@0 { > compatible = "google,cros-ec-spi"; > }; > } > > For rpmsg, the rpmsg_device are dynamically created from the request > of the SCP, and then a matching rpmsg_driver is used when found. > Currently without the cros-ec-rpmsg being in the device tree, the cros_ec_rpmsg > module would need to be manually loaded by modprobe. > > To follow what SPI/I2C does, the device tree would look like: > scp { > compatible = "mediatek,mt8183-scp"; > mt8183-rpmsg { > compatible = "mediatek,mt8183-rpmsg"; > cros_ec_rpmsg { > compatible = "google,cros-ec-rpmsg"; > }; > }; > }; > But the rpmsg driver would not actually create those rpmsg_device on probe, but > only look at those sub node and load the corresponding rpmsg_driver modules. > When requested by SCP to create the rpmsg_device, it would find a matching > rpmsg_driver independent on how the device tree looks. > > So my question is, should these dynamically created rpmsg_device be listed on > device tree? > I think that right now that's our main problem, how to properly instantiate all this stuff. One approach that I like is the one used in the TI PRU ICSS, they create a pruss_soc_bus driver with the purpose to allow the child nodes to be bound. I suspect that something similar would work, but I need to look in more detail. See [1] [1] https://lkml.org/lkml/2018/11/22/948 Cheers, Enric > > > > > +static const struct rpmsg_device_id cros_ec_rpmsg_device_id[] = { > > > + { .name = "cros-ec-rpmsg", }, > > > + { /* sentinel */ }, > > > > I got convinced that the '/* sentinel */' comment doesn't means > > anything, so use { } only here, remove the comment and the last comma > > (there is nothing to separate) > > + { } > > > > > +}; > > > + > > > +static struct rpmsg_driver cros_ec_driver_rpmsg = { > > > + .drv.name = KBUILD_MODNAME, > > > > And something like this here > > .drv = { > > .name = "cros-ec-rpmsg", > > .of_match_table = cros_ec_rpmsg_of_match, > > }, > > > > > + .id_table = cros_ec_rpmsg_device_id, > > > + .probe = cros_ec_rpmsg_probe, > > > + .remove = cros_ec_rpmsg_remove, > > > + .callback = cros_ec_rpmsg_callback, > > > +}; > > > + > > > +module_rpmsg_driver(cros_ec_driver_rpmsg); > > > + > > > +MODULE_LICENSE("GPL v2"); > > > +MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)"); > > > diff --git a/include/linux/mfd/cros_ec.h b/include/linux/mfd/cros_ec.h > > > index de8b588c8776da..fd297cf8f97295 100644 > > > --- a/include/linux/mfd/cros_ec.h > > > +++ b/include/linux/mfd/cros_ec.h > > > @@ -24,6 +24,7 @@ > > > > > > #define CROS_EC_DEV_NAME "cros_ec" > > > #define CROS_EC_DEV_PD_NAME "cros_pd" > > > +#define CROS_EC_DEV_SCP_NAME "cros_scp" > > > > I think this definition is not needed. > > > > > > > > /* > > > * The EC is unresponsive for a time after a reboot command. Add a > > > diff --git a/include/linux/mfd/cros_ec_commands.h b/include/linux/mfd/cros_ec_commands.h > > > index fc91082d4c357b..3e5da6e93b2f42 100644 > > > --- a/include/linux/mfd/cros_ec_commands.h > > > +++ b/include/linux/mfd/cros_ec_commands.h > > > @@ -856,6 +856,8 @@ enum ec_feature_code { > > > EC_FEATURE_RTC = 27, > > > /* EC supports CEC commands */ > > > EC_FEATURE_CEC = 35, > > > + /* The MCU exposes a SCP */ > > > + EC_FEATURE_SCP = 39, > > > > Same here, I think this is not needed. > > > }; > > > > > > #define EC_FEATURE_MASK_0(event_code) (1UL << (event_code % 32)) > > > -- > > > 2.20.1.415.g653613c723-goog > > > > > > > Thanks, > > Enric