All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Wang Yang" <yang.wang@deepcomputing.io>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 5/9] platform/chrome: Add SpacemiT K3 eSPI EC transport
Date: Wed, 18 Nov 2026 09:33:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-220580@kernel.org> (raw)
In-Reply-To: <20261118085742.1817536-6-yang.wang@deepcomputing.io>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] platform/chrome: missing wakeup init in cros_ec_espi_probe()
- [Medium] platform/chrome: missing eSPI window bounds checks in
  lpc_to_espi_offset()
- [Medium] platform/chrome: race on event_data in cros_ec_espi_probe()
  IRQ drain
- [Medium] platform/chrome: missing post-ID delay in
  cros_ec_espi_wait_ready()
- [Low] platform/chrome: spurious error log on -EAGAIN in
  cros_ec_spacemit_k3
--

commit b0c80322a90e8c8d2c53ee26b39169afce373af6
Author: Wang Yang <yang.wang@deepcomputing.io>

platform/chrome: Add SpacemiT K3 eSPI EC transport

This commit adds a ChromeOS EC transport driver for the SpacemiT K3 eSPI
peripheral shared-memory window, supporting host command protocol versions
2 and 3, controller link recovery, and EC event interrupts.

> diff --git a/drivers/platform/chrome/cros_ec_spacemit_k3.c b/drivers/platform/chrome/cros_ec_spacemit_k3.c
> new file mode 100644
> index 0000000000000..b3010e2bff6a5
> --- /dev/null
> +++ b/drivers/platform/chrome/cros_ec_spacemit_k3.c
[ ... ]
> +/* eSPI shared memory offsets */
> +#define ESPI_HOST_CMD_OFFSET 0x000 /* EC_LPC_ADDR_HOST_CMD -> 0x000 */
> +#define ESPI_HOST_DATA_OFFSET 0x001 /* EC_LPC_ADDR_HOST_DATA -> 0x001 */
> +#define ESPI_HOST_ARGS_OFFSET 0x010 /* EC_LPC_ADDR_HOST_ARGS -> 0x010 */
> +#define ESPI_HOST_PARAM_OFFSET 0x018 /* EC_LPC_ADDR_HOST_PARAM -> 0x018 */
> +#define ESPI_HOST_PACKET_OFFSET 0x100 /* EC_LPC_ADDR_HOST_PACKET -> 0x100 */
> +#define ESPI_MEMMAP_OFFSET 0x200 /* EC_LPC_ADDR_MEMMAP -> 0x200 */
[ ... ]
> +/* Convert LPC address to eSPI shared memory offset */
> +static inline u32 lpc_to_espi_offset(struct cros_ec_device *ec, u32 lpc_addr)
> +{
> +	switch (lpc_addr & 0xFF00) {
> +	case 0x200: /* HOST_CMD/DATA region */
> +		if (lpc_addr == EC_LPC_ADDR_HOST_CMD)
> +			return ESPI_HOST_CMD_OFFSET;
> +		else if (lpc_addr == EC_LPC_ADDR_HOST_DATA)
> +			return ESPI_HOST_DATA_OFFSET;
> +		break;
> +	case 0x800: /* HOST_ARGS/PARAM/PACKET region */
> +		/* Protocols 2 and 3 use the same LPC addresses. */
> +		if (ec->proto_version > 2)
> +			return ESPI_HOST_PACKET_OFFSET +
> +			       (lpc_addr - EC_LPC_ADDR_HOST_PACKET);
> +		else if (lpc_addr >= EC_LPC_ADDR_HOST_ARGS &&
> +			 lpc_addr < EC_LPC_ADDR_HOST_PARAM)
> +			return ESPI_HOST_ARGS_OFFSET +
> +			       (lpc_addr - EC_LPC_ADDR_HOST_ARGS);
> +		else if (lpc_addr >= EC_LPC_ADDR_HOST_PARAM)
> +			return ESPI_HOST_PARAM_OFFSET +
> +			       (lpc_addr - EC_LPC_ADDR_HOST_PARAM);
> +		break;
> +	case 0x900: /* MEMMAP region */
> +		return ESPI_MEMMAP_OFFSET + (lpc_addr - EC_LPC_ADDR_MEMMAP);
> +	}
> +
> +	/* Default mapping for unknown addresses */
> +	pr_warn_once("Unknown LPC address 0x%x, using direct offset\n",
> +		     lpc_addr);
> +	return lpc_addr & 0xFF;
> +}

[Severity: Medium]
Can lpc_to_espi_offset() read and write past the parameter and packet
sub-windows into adjacent regions of espi_shared_mem_base?

For protocol v2, ESPI_HOST_PARAM_OFFSET starts at 0x018 and only has 232
bytes before ESPI_HOST_PACKET_OFFSET at 0x100, but
cros_ec_cmd_xfer_espi_once() allows msg->outsize and msg->insize up to
EC_PROTO2_MAX_PARAM_SIZE (252 bytes).  A transfer of 233..252 bytes maps
EC_LPC_ADDR_HOST_PARAM + i to 0x100..0x113, overflowing into the packet
window.

Similarly, for protocol v3, cros_ec_pkt_xfer_espi_once() does not check
that the request length returned by cros_ec_prepare_tx() or
sizeof(response) + response.data_len fits within the 256-byte packet window
(EC_LPC_HOST_PACKET_SIZE).  When a packet exceeds 256 bytes,
EC_LPC_ADDR_HOST_PACKET + i reaches 0x900 and lpc_to_espi_offset()
translates it into ESPI_MEMMAP_OFFSET (0x200..0x2ff) and then wraps via
the default (lpc_addr & 0xff) fallback to 0x000, overwriting
ESPI_HOST_CMD_OFFSET and ESPI_HOST_DATA_OFFSET.

[ ... ]

> +static int cros_ec_espi_wait_ready(struct cros_ec_device *ec, const char *reason)
> +{
> +	struct device *dev = ec->dev;
> +	unsigned long timeout = jiffies + msecs_to_jiffies(ESPI_READY_TIMEOUT_MS);
> +	unsigned long next_recover =
> +		jiffies + msecs_to_jiffies(ESPI_RECOVERY_RETRY_INTERVAL_MS);
> +	u8 buf[2] = {};
> +	int ret = 0;
> +
> +	do {
> +		ret = cros_ec_espi_read_id(ec, buf);
> +		if (ret >= 0 && cros_ec_espi_valid_id(buf))
> +			return 0;

[Severity: Medium]
Does cros_ec_espi_wait_ready() return before the EC has finished
initializing its command window?

In cros_ec_espi_probe(), the driver sleeps for 100 ms after seeing the EC
ID because the EC still initializes its command window after publishing
EC_MEMMAP_ID.  During runtime recovery, cros_ec_espi_xfer_retry() calls
cros_ec_espi_recover_controller() -> cros_ec_espi_wait_ready(), which
returns 0 as soon as 'E' and 'C' appear without that 100 ms delay, so the
single command retry in cros_ec_espi_xfer_retry() is sent immediately while
the command window may still be initializing.

[ ... ]

> +static int cros_ec_pkt_xfer_espi_once(struct cros_ec_device *ec,
> +				      struct cros_ec_command *msg)
> +{
[ ... ]
> +	/* Check result */
> +	ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_DATA, 1, &sum);
> +	if (ret < 0) {
> +		dev_err(ec->dev, "Read from HOST_DATA failed: %d\n", ret);
> +		goto done;
> +	}
> +
> +	msg->result = sum;
> +	ret = cros_ec_check_result(ec, msg);
> +	if (ret) {
> +		dev_err(ec->dev, "cros_ec_check_result failed: %d\n", ret);
> +		goto done;
> +	}

[Severity: Low]
Should cros_ec_pkt_xfer_espi_once() and cros_ec_cmd_xfer_espi_once() avoid
logging dev_err() when cros_ec_check_result() returns non-zero?

cros_ec_check_result() only returns non-zero (-EAGAIN) when msg->result is
EC_RES_IN_PROGRESS, which is normal for slow commands that
cros_ec_send_command() polls until completion via
cros_ec_wait_until_complete().  Logging dev_err() here prints an error on
every status poll while a command is in progress.

[ ... ]

> +static int cros_ec_espi_probe(struct platform_device *pdev)
> +{
[ ... ]
> +	if (irq > 0) {
> +		ec->irq = irq;
> +		ret = devm_request_threaded_irq(dev, irq, cros_ec_espi_irq_handler,
> +						cros_ec_irq_thread, IRQF_ONESHOT,
> +						"chromeos-ec", ec);
> +		if (ret) {
> +			cros_ec_unregister(ec);
> +			return dev_err_probe(dev, ret, "failed to request EC IRQ\n");
> +		}
> +
> +		/* Drain events queued before the edge-triggered IRQ was ready. */
> +		if (ec->mkbp_event_supported)
> +			cros_ec_irq_thread(irq, ec);
> +	}
> +
> +	return 0;
> +}

[Severity: Medium]
Can calling cros_ec_irq_thread() directly here race with the newly enabled
threaded IRQ handler on ec->event_data and ec->event_size?

devm_request_threaded_irq() enables the interrupt before the probe thread
calls cros_ec_irq_thread(irq, ec):

CPU0 (probe)                      CPU1 (irq/<irq>-chromeos-ec)
cros_ec_espi_probe()
  devm_request_threaded_irq()
  cros_ec_irq_thread()
    cros_ec_handle_event()
      cros_ec_get_next_event()
        get_next_event_xfer()
          ec_dev->event_data = ...
                                  cros_ec_irq_thread()
                                    ...
                                      get_next_event_xfer()
                                        ec_dev->event_data = ...
        ec_dev->event_data.event_type &= ...
      blocking_notifier_call_chain()  <-- reads overwritten event_data

In get_next_event_xfer() and cros_ec_get_next_event(), ec_dev->lock only
protects cros_ec_cmd_xfer_status(); updates to ec_dev->event_size and
ec_dev->event_data and the subsequent notifier callbacks run unlocked.
Waking the IRQ thread with irq_wake_thread(irq, ec) instead of calling
cros_ec_irq_thread() directly would serialize the initial drain with IRQ
delivery.

[ ... ]

> +static int cros_ec_espi_suspend_late(struct device *dev)
> +{
> +	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
> +
> +	return cros_ec_suspend_late(ec_dev);
> +}

[Severity: Medium]
Does EC wakeup from suspend work without calling device_init_wakeup() in
cros_ec_espi_probe()?

cros_ec_suspend_late() -> cros_ec_disable_irq() and cros_ec_handle_event()
check device_may_wakeup(ec_dev->dev) before calling enable_irq_wake() and
pm_wakeup_event():

drivers/platform/chrome/cros_ec.c:cros_ec_disable_irq() {
    ...
	if (device_may_wakeup(dev))
		ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
	else
		ec_dev->wake_enabled = false;

	disable_irq(ec_dev->irq);
}

Unlike the I2C core, the platform bus does not automatically initialize
wakeup capability from the devicetree wakeup-source property, so
device_may_wakeup(ec->dev) is always false and the EC IRQ is disabled
during suspend without wake enabled.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118085742.1817536-1-yang.wang@deepcomputing.io?part=5

  reply	other threads:[~2026-11-18  9:33 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:57 [PATCH 0/9] Add SpacemiT K3 FML13V05 EC keyboard support Wang Yang
2026-11-18  8:57 ` Wang Yang
2026-11-18  8:57 ` [PATCH 1/9] dt-bindings: bus: Add SpacemiT K3 eSPI controller Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  9:18   ` sashiko-bot
2026-11-18  8:57 ` [PATCH 2/9] dt-bindings: embedded-controller: Add SpacemiT K3 EC window Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  8:57 ` [PATCH 3/9] bus: Add SpacemiT K3 eSPI controller support Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  9:23   ` sashiko-bot
2026-11-18  8:57 ` [PATCH 4/9] platform/chrome: Allow ChromeOS platform support on RISC-V Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  8:57 ` [PATCH 5/9] platform/chrome: Add SpacemiT K3 eSPI EC transport Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  9:33   ` sashiko-bot [this message]
2026-11-18  8:57 ` [PATCH 6/9] riscv: dts: spacemit: Add K3 eSPI pin group Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  9:08   ` sashiko-bot
2026-11-18  8:57 ` [PATCH 7/9] riscv: dts: spacemit: Describe the K3 eSPI controller Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  9:25   ` sashiko-bot
2026-11-18  8:57 ` [PATCH 8/9] riscv: dts: spacemit: Enable the FML13V05 EC keyboard Wang Yang
2026-11-18  8:57   ` Wang Yang
2026-11-18  9:20   ` sashiko-bot
2026-11-18  8:57 ` [PATCH 9/9] riscv: defconfig: Enable the FML13V05 EC keyboard modules Wang Yang
2026-11-18  8:57   ` Wang Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-220580@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yang.wang@deepcomputing.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.