mmc: core: Keep the card powered across suspend when firmware needs it live
Subsystems (2)
v14 cover mmc: core: Keep the card powered across suspend when firmware needs it live
Background: on brcmstb boards with a Kioxia 016G01 eMMC, firmware accesses the card directly during resume from Suspend-to-DRAM, before the kernel's own resume path runs, in order to load boot code using hard wired logic that is not field updatable. The card needs to stay powered and responsive for that access to succeed, and since it is never power-cycled, it also needs to be reset before the kernel reuses it after resume.
v13: https://lore.kernel.org/linux-mmc/[email protected]/
Changes in v14:
- Patch 6: Clear MMC_PM_KEEP_POWER in mmc_host_class_complete() when MMC_CAP2_RESET_AT_RESUME is set. The flag is set in mmc_host_class_prepare() but was only cleared in mmc_resume(), so it stayed set if system suspend was aborted after prepare (reported by Sashiko).
- Patches 1-5: No changes.
- Tested with CQE enabled.
Review feedback addressed so far, including Sashiko's AI review comments:
- v14, patch 6: MMC_PM_KEEP_POWER is set in mmc_host_class_prepare() and was only cleared in mmc_resume(). It is now also cleared in mmc_host_class_complete(), so it doesn't stay set after an aborted system suspend.
- v13, patch 3: CQE is turned off explicitly when the card is kept powered, as the cache flush may not send any command that does so.
- v12, patch 6: An error from _mmc_hw_reset() in mmc_resume() is propagated instead of being dropped.
- v12, patch 6: reset-card-at-resume without keep-power-in-suspend makes the host probe fail in mmc_validate_host_caps(), instead of only warning, as the card would otherwise be powered off and system suspend refused (Ulf).
- v11, patch 6: The host is claimed around the card reset in mmc_resume(), as it sends commands to the card.
- Earlier series, before Ulf's rework: Sashiko noted that the DT schema allowed reset-card-at-resume without keep-power-in-suspend. Patch 2 keeps the "dependencies" entry that enforces the pairing.
Tested on a brcmstb board with a Kioxia 016G01 eMMC, running S3 suspend/resume (pml -d) with the DT properties set as below:
keep-power-in-suspend reset-card-at-resume Result no no board resets on resume yes no board resets on resume no yes host probe fails with -EINVAL yes yes resumes, card re-enumerated
Kamal Dasu (2):
dt-bindings: mmc: Extend keep-power-in-suspend beyond SDIO
dt-bindings: mmc: Add reset-card-at-resume property
Ulf Hansson (4):
mmc: core: Add option to keep an eMMC powered on at system suspend
mmc: core: Split sdio_set_host_pm_flags()
mmc: core: Move and split reset functions for eMMC
mmc: core: Add support for resetting an eMMC at system resume
.../bindings/mmc/mmc-controller-common.yaml | 10 +- drivers/mmc/core/core.c | 10 ++ drivers/mmc/core/core.h | 1 + drivers/mmc/core/host.c | 26 ++++ drivers/mmc/core/mmc.c | 111 ++++++++++++------ drivers/mmc/core/sdio_io.c | 7 +- include/linux/mmc/host.h | 1 + 7 files changed, 123 insertions(+), 43 deletions(-)
PATCH v14 1/6 dt-bindings: mmc: Extend keep-power-in-suspend beyond SDIO
keep-power-in-suspend is currently documented as SDIO-only, but the requirement it describes -- preserving card power across a suspend/resume cycle -- applies just as well to any card type. Drop the SDIO-only restriction so eMMC and SD platforms can use it too.
--- .../devicetree/bindings/mmc/mmc-controller-common.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/devicetree/bindings/mmc/mmc-controller-common.yaml +1 -1
@@ -301,7 +301,7 @@ properties: keep-power-in-suspend: $ref: /schemas/types.yaml#/definitions/flag description: - SDIO only. Preserves card power during a suspend/resume cycle. + Preserves card power during a suspend/resume cycle. wakeup-source: $ref: /schemas/types.yaml#/definitions/flag
PATCH v14 2/6 dt-bindings: mmc: Add reset-card-at-resume property
In reply to Kamal DasuOn some platforms, firmware or other hardware accesses the card during suspend/resume, before the kernel's own resume path has run, leaving the card in a state the kernel can no longer assume it knows. Add a flag property so such boards can tell the mmc core the card needs to be reset before it can be used again.
This is expected to be paired with keep-power-in-suspend on boards whose firmware needs the card to stay powered and live throughout
suspend: since the card is never power-cycled, nothing else would force it back to a known state on resume.
--- .../devicetree/bindings/mmc/mmc-controller-common.yaml | 8 ++++++++ 1 file changed, 8 insertions(+)
Documentation/devicetree/bindings/mmc/mmc-controller-common.yaml +8 -0
@@ -303,6 +303,13 @@ properties: description: Preserves card power during a suspend/resume cycle. + reset-card-at-resume: + $ref: /schemas/types.yaml#/definitions/flag + description: + The HW/FW may have accessed the card during suspend/resume, + leaving it in an unknown state. Hence, before the card can be + used, it must be reset. + wakeup-source: $ref: /schemas/types.yaml#/definitions/flag description:
@@ -371,5 +378,6 @@ patternProperties: dependencies: cd-debounce-delay-ms: [ cd-gpios ] fixed-emmc-driver-type: [ non-removable ] + reset-card-at-resume: [ keep-power-in-suspend ] additionalProperties: true
PATCH v14 3/6 mmc: core: Add option to keep an eMMC powered on at system suspend
In reply to Kamal DasuFrom: Ulf Hansson <[email protected]>
The DT property keep-power-in-suspend, allows us to specify whether the platform is capable of keeping the power to the card during system suspend. Up until now, this has been restricted to be used for SDIO cards only, but let's add the initial support to allow this for eMMC too.
As the card is neither powered off nor sent a command that would switch off the command queue engine when the cache flush is skipped, turn CQE off explicitly if it is active before leaving the card powered on.
--- drivers/mmc/core/mmc.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
drivers/mmc/core/mmc.c +25 -1
@@ -38,6 +38,7 @@ enum mmc_poweroff_type {MMC_POWEROFF_SHUTDOWN ,MMC_POWEROFF_UNDERVOLTAGE ,MMC_POWEROFF_UNBIND , + MMC_NO_POWEROFF_SUSPEND, }; static const unsigned int tran_exp[] = {
@@ -2157,6 +2158,17 @@ static int _mmc_suspend(struct mmc_host *host, enum mmc_poweroff_type pm_type) goto out; } +if (pm_type ==MMC_NO_POWEROFF_SUSPEND ) { +if (host->cqe_on) + host->cqe_ops->cqe_off (host); +if (host->retune_period) { +mmc_retune_timer_stop (host); +mmc_retune_needed (host); + } +mmc_card_set_suspended (host->card); + goto out; + } +if (mmc_card_can_poweroff_notify (host->card) &&mmc_host_can_poweroff_notify (host, pm_type)) err =mmc_poweroff_notify (host->card, notify_type);
@@ -2193,9 +2205,15 @@ static void mmc_remove(struct mmc_host *host)*/ static int mmc_suspend (struct mmc_host *host) { +enum mmc_poweroff_type pm_type =MMC_POWEROFF_SUSPEND ;int err; - err =_mmc_suspend (host,MMC_POWEROFF_SUSPEND ); +if (mmc_card_keep_power (host)) { +pm_runtime_resume (&host->card->dev); + pm_type =MMC_NO_POWEROFF_SUSPEND ; + } + + err =_mmc_suspend (host, pm_type);if (!err) {pm_runtime_disable (&host->card->dev);pm_runtime_set_suspended (&host->card->dev);
@@ -2260,7 +2278,13 @@ static int mmc_shutdown(struct mmc_host *host)*/ static int mmc_resume (struct mmc_host *host) { +if (mmc_card_keep_power (host)) { +pm_runtime_set_active (&host->card->dev); +mmc_card_clr_suspended (host->card); + } +pm_runtime_enable (&host->card->dev); + host->pm_flags &= ~MMC_PM_KEEP_POWER ;return 0 ; }
PATCH v14 4/6 mmc: core: Split sdio_set_host_pm_flags()
In reply to Kamal DasuFrom: Ulf Hansson <[email protected]>
To prepare for a subsequent change, let's split up sdio_set_host_pm_flags() allowing the second part of it to be used as an internal function for the core.
--- drivers/mmc/core/core.c | 10 ++++++++++ drivers/mmc/core/core.h | 1 + drivers/mmc/core/sdio_io.c | 7 +------ 3 files changed, 12 insertions(+), 6 deletions(-)
drivers/mmc/core/core.c +10 -0
@@ -1422,6 +1422,16 @@ int mmc_handle_undervoltage(struct mmc_host *host)return host->bus_ops->handle_undervoltage (host); } +int mmc_set_host_pm_flags (struct mmc_host *host,mmc_pm_flag_t flags) +{ +if (flags & ~host->pm_caps) +return -EINVAL ; + +/* function suspend methods are serialized, hence no lock needed */ + host->pm_flags |= flags; +return 0 ; +} +/* * Assign a mmc bus handler to a host. Only one bus handler may control a * host at any given time.
drivers/mmc/core/core.h +1 -0
@@ -64,6 +64,7 @@ int mmc_handle_undervoltage(struct mmc_host *host);void mmc_regulator_register_undervoltage_notifier (struct mmc_host *host);void mmc_regulator_unregister_undervoltage_notifier (struct mmc_host *host);void mmc_undervoltage_workfn (struct work_struct *work); +int mmc_set_host_pm_flags (struct mmc_host *host,mmc_pm_flag_t flags);static inline void mmc_delay (unsigned int ms) {
drivers/mmc/core/sdio_io.c +1 -6
@@ -726,12 +726,7 @@ int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags) host = func->card->host; -if (flags & ~host->pm_caps) -return -EINVAL ; - -/* function suspend methods are serialized, hence no lock needed */ - host->pm_flags |= flags; -return 0 ; +return mmc_set_host_pm_flags (host, flags); }EXPORT_SYMBOL_GPL (sdio_set_host_pm_flags);
PATCH v14 5/6 mmc: core: Move and split reset functions for eMMC
In reply to Kamal DasuFrom: Ulf Hansson <[email protected]>
To prepare for a subsequent change and to simplify review, let's move the function for resetting the eMMC card a few lines up in the file.
While at it, split the cache flush out of _mmc_hw_reset() into a new mmc_hw_reset_recovery() function and use that for the ->hw_reset() bus operation. This keeps the flush for recovery, where it was intended, but allows the card to be reset without it, by calling _mmc_hw_reset() directly. That is needed when resetting the card at system resume, as the cache has already been flushed at suspend and the flush would only fail with a timeout, after the card has been accessed by firmware.
There are no functional changes.
--- drivers/mmc/core/mmc.c | 73 ++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 34 deletions(-)
drivers/mmc/core/mmc.c +39 -34
@@ -2273,6 +2273,44 @@ static int mmc_shutdown(struct mmc_host *host)return err; } +static bool mmc_card_can_reset (struct mmc_card *card) +{ +u8 rst_n_function; + + rst_n_function = card->ext_csd.rst_n_function; +return ((rst_n_function &EXT_CSD_RST_N_EN_MASK ) ==EXT_CSD_RST_N_ENABLED ); +} + +static int _mmc_hw_reset (struct mmc_host *host) +{ +struct mmc_card *card = host->card; + +if ((host->caps &MMC_CAP_HW_RESET ) && host->ops->card_hw_reset && +mmc_card_can_reset (card)) { +/* If the card accept RST_n signal, send it. */ +mmc_set_clock (host, host->f_init); + host->ops->card_hw_reset (host); +/* Set initial state and call mmc_set_ios */ +mmc_set_initial_state (host); + }else { +/* Do a brute force power cycle */ +mmc_power_cycle (host, card->ocr); +mmc_pwrseq_reset (host); + } +return mmc_init_card (host, card->ocr, card); +} + +static int mmc_hw_reset_recovery (struct mmc_host *host) +{ +/* +* In the case of recovery, we can't expect flushing the cache to work +* always, but we have a go and ignore errors. +*/ +_mmc_flush_cache (host); + +return _mmc_hw_reset (host); +} +/* * Callback for resume. */
@@ -2321,39 +2359,6 @@ static int mmc_runtime_resume(struct mmc_host *host)return 0 ; } -static bool mmc_card_can_reset (struct mmc_card *card) -{ -u8 rst_n_function; - - rst_n_function = card->ext_csd.rst_n_function; -return ((rst_n_function &EXT_CSD_RST_N_EN_MASK ) ==EXT_CSD_RST_N_ENABLED ); -} - -static int _mmc_hw_reset (struct mmc_host *host) -{ -struct mmc_card *card = host->card; - -/* -* In the case of recovery, we can't expect flushing the cache to work -* always, but we have a go and ignore errors. -*/ -_mmc_flush_cache (host); - -if ((host->caps &MMC_CAP_HW_RESET ) && host->ops->card_hw_reset && -mmc_card_can_reset (card)) { -/* If the card accept RST_n signal, send it. */ -mmc_set_clock (host, host->f_init); - host->ops->card_hw_reset (host); -/* Set initial state and call mmc_set_ios */ -mmc_set_initial_state (host); - }else { -/* Do a brute force power cycle */ -mmc_power_cycle (host, card->ocr); -mmc_pwrseq_reset (host); - } -return mmc_init_card (host, card->ocr, card); -} -/** * _mmc_handle_undervoltage - Handle an undervoltage event for MMC/eMMC devices * @host: MMC host structure
@@ -2412,7 +2417,7 @@ static const struct mmc_bus_ops mmc_ops = { .runtime_resume = mmc_runtime_resume, .alive = mmc_alive, .shutdown = mmc_shutdown, - .hw_reset = _mmc_hw_reset, + .hw_reset = mmc_hw_reset_recovery, .cache_enabled = _mmc_cache_enabled, .flush_cache = _mmc_flush_cache, .handle_undervoltage = _mmc_handle_undervoltage,
PATCH v14 6/6 mmc: core: Add support for resetting an eMMC at system resume
In reply to Kamal DasuFrom: Ulf Hansson <[email protected]>
The DT property reset-card-at-resume specifies that we need to make a reset of an eMMC card at system resume, assuming it also has stayed powered on during system suspend.
To implement support for this, let's start by parsing for the new DT property and set the new bit MMC_CAP2_RESET_AT_RESUME if it's found. Based on the property, we must also set MMC_PM_KEEP_POWER in the pm_flags for the host, to keep the eMMC powered on during system suspend.
Finally, to manage the actual reset during system resume, let's extend mmc_resume() to support the needed behaviour.
The property only makes sense together with keep-power-in-suspend, as the card would otherwise be powered off and system suspend is refused. Validate that in mmc_validate_host_caps(), to make the host probe fail upfront on such a misconfiguration.
While at it, claim the host around the reset in mmc_resume(), as it sends commands to the card, and propagate any error from the reset so that a failure to re-initialize the card is reported by the resume path.
Also clear MMC_PM_KEEP_POWER in mmc_host_class_complete(), as mmc_resume() isn't invoked if system suspend is aborted after mmc_host_class_prepare() has set the flag.
--- drivers/mmc/core/host.c | 26 ++++++++++++++++++++++++++ drivers/mmc/core/mmc.c | 12 +++++++++++- include/linux/mmc/host.h | 1 + 3 files changed, 38 insertions(+), 1 deletion(-)
drivers/mmc/core/host.c +26 -0
@@ -48,6 +48,14 @@ static int mmc_host_class_prepare(struct device *dev)if (host->bus_ops->pre_suspend)return host->bus_ops->pre_suspend (host); +/* +* If it's required to make a reset of the card at system resume, this +* indirectly also means that we must keep the card powered on during +* system suspend. +*/ +if (host->caps2 &MMC_CAP2_RESET_AT_RESUME ) +return mmc_set_host_pm_flags (host,MMC_PM_KEEP_POWER ); +return 0 ; }
@@ -55,6 +63,15 @@ static void mmc_host_class_complete(struct device *dev) {struct mmc_host *host =cls_dev_to_mmc_host (dev); +/* +* MMC_PM_KEEP_POWER is set in ->prepare() and normally cleared by the +* bus ->resume() callback. If system suspend is aborted after +* ->prepare(), ->resume() isn't invoked, so clear the flag here to not +* leave it set at runtime. +*/ +if (host->caps2 &MMC_CAP2_RESET_AT_RESUME ) + host->pm_flags &= ~MMC_PM_KEEP_POWER ; +_mmc_detect_change (host,0 , false); }
@@ -375,6 +392,8 @@ int mmc_of_parse(struct mmc_host *host) host->pm_caps |=MMC_PM_KEEP_POWER ;if (device_property_read_bool (dev,"wakeup-source" )) host->pm_caps |=MMC_PM_WAKE_SDIO_IRQ ; +if (device_property_read_bool (dev,"reset-card-at-resume" )) + host->caps2 |=MMC_CAP2_RESET_AT_RESUME ;if (device_property_read_bool (dev,"mmc-ddr-3_3v" )) host->caps |=MMC_CAP_3_3V_DDR ;if (device_property_read_bool (dev,"mmc-ddr-1_8v" ))
@@ -620,6 +639,13 @@ static int mmc_validate_host_caps(struct mmc_host *host)return -EINVAL ; } +/* Resetting the card at resume requires that it stays powered on */ +if (caps2 &MMC_CAP2_RESET_AT_RESUME && + !(host->pm_caps &MMC_PM_KEEP_POWER )) { +dev_warn (dev,"reset-card-at-resume requires keep-power-in-suspend\n" ); +return -EINVAL ; + } +/* UHS/DDR/HS200 modes require at least 4-bit bus */ if (!(caps & (MMC_CAP_4_BIT_DATA |MMC_CAP_8_BIT_DATA )) && ((caps & (MMC_CAP_UHS |MMC_CAP_DDR )) || (caps2 &MMC_CAP2_HS200 ))) {
drivers/mmc/core/mmc.c +11 -1
@@ -2316,14 +2316,24 @@ static int mmc_hw_reset_recovery(struct mmc_host *host)*/ static int mmc_resume (struct mmc_host *host) { +int err =0 ; +if (mmc_card_keep_power (host)) {pm_runtime_set_active (&host->card->dev);mmc_card_clr_suspended (host->card); }pm_runtime_enable (&host->card->dev); + +if (mmc_card_keep_power (host) && + host->caps2 &MMC_CAP2_RESET_AT_RESUME ) { +mmc_claim_host (host); + err =_mmc_hw_reset (host); +mmc_release_host (host); + } + host->pm_flags &= ~MMC_PM_KEEP_POWER ; -return 0 ; +return err; }/*
include/linux/mmc/host.h +1 -0
@@ -430,6 +430,7 @@ struct mmc_host {u32 caps2;/* More host capabilities */ #define MMC_CAP2_BOOTPART_NOACC (1 << 0)/* Boot partition no access */ +#define MMC_CAP2_RESET_AT_RESUME BIT(1)/* Reset the card at system resume */ #define MMC_CAP2_FULL_PWR_CYCLE (1 << 2)/* Can do full power cycle */ #define MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND (1 << 3)/* Can do full power cycle in suspend */ #define MMC_CAP2_HS200_1_8V_SDR (1 << 5)/* can support */