Skip to contentSkip to conversation

Use runtime-pm to switch sdio to 1-bit when idle

PATCH In review NeilBrown 2 participants 6 messages Mar 24, 2015 MMC, SD and SDIO

lore
Share & tools
lore

NeilBrown

author
#0 lore raw

cover Use runtime-pm to switch sdio to 1-bit when idle

This is a proof-of-concept hack to demonstrate what I was trying to describe earlier.
The last patch, in particular, takes some very ugly short-cuts which I would never dream of proposing for upstream inclusion.

It seems to work, but I haven't tested thoroughly. I may not even be doing what I think it is doing...

Comments?

Thanks,
NeilBrown
---

NeilBrown (4):
mmc: core: fold mmc_set_bus_width calls into sdio_enable_4bit_bus. mmc/core: add pm_runtime tracking to mmc_host devices. mmc/sdio: keep device awake when interrupts expected in 4bit mode mmc/host: use runtime_pm to switch to 1-bit mode

 drivers/mmc/core/core.c |    5 +++
 drivers/mmc/core/host.c |   78 ++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/mmc/core/sdio.c |   41 +++++++++++--------------
 3 files changed, 100 insertions(+), 24 deletions(-)

--
Signature

author
#1 lore raw

PATCH 1/4 mmc: core: fold mmc_set_bus_width calls into sdio_enable_4bit_bus.

From: NeilBrown <[email protected]>

Every call to sdio_enable_4bit_bus is followed (on success) but a call to mmc_set_bus_width().

To simplify the code, include those calls directly in sdio_enable_4bit_bus().

---
 drivers/mmc/core/sdio.c |   32 ++++++++++++--------------------
 1 file changed, 12 insertions(+), 20 deletions(-)
drivers/mmc/core/sdio.c +12 -20
index ce6cc47206b0..5bc6c7dbbd60 100644
@@ -293,19 +293,22 @@ static int sdio_enable_4bit_bus(struct mmc_card *card)
 	int err;
 
 	if (card->type == MMC_TYPE_SDIO)
-		return sdio_enable_wide(card);
-
-	if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
-		(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
+		err = sdio_enable_wide(card);
+	else if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
+		 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
 		err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
 		if (err)
 			return err;
+		err = sdio_enable_wide(card);
+		if (err <= 0)
+			mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
 	} else
 		return 0;
 
-	err = sdio_enable_wide(card);
-	if (err <= 0)
-		mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
+	if (err > 0) {
+		mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
+		err = 0;
+	}
 
 	return err;
 }
@@ -547,13 +550,8 @@ static int mmc_sdio_init_uhs_card(struct mmc_card *card)
 	/*
 	 * Switch to wider bus (if supported).
 	 */
-	if (card->host->caps & MMC_CAP_4_BIT_DATA) {
+	if (card->host->caps & MMC_CAP_4_BIT_DATA)
 		err = sdio_enable_4bit_bus(card);
-		if (err > 0) {
-			mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
-			err = 0;
-		}
-	}
 
 	/* Set the driver strength for the card */
 	sdio_select_driver_type(card);
@@ -803,9 +801,7 @@ try_again:
 		 * Switch to wider bus (if supported).
 		 */
 		err = sdio_enable_4bit_bus(card);
-		if (err > 0)
-			mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
-		else if (err)
+		if (err)
 			goto remove;
 	}
 finish:
@@ -983,10 +979,6 @@ static int mmc_sdio_resume(struct mmc_host *host)
 	} else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
 		/* We may have switched to 1-bit mode during suspend */
 		err = sdio_enable_4bit_bus(host->card);
-		if (err > 0) {
-			mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
-			err = 0;
-		}
 	}
 
 	if (!err && host->sdio_irqs) {
author
#3 lore raw

PATCH 2/4 mmc/core: add pm_runtime tracking to mmc_host devices.

In reply to NeilBrown

Enable runtime pm for mmc_host devices, and take a reference while the host is claimed.

Use an autosuspend timeout so that the device isn't put to sleep until we have been idle for a while.

Set the parent to ignore children, so the PM status of the host does not affect the controller at all.

This functionality will be used in a future patch to allow commands to be sent to the device when the host is idle.

---
 drivers/mmc/core/core.c |    5 +++++
 drivers/mmc/core/host.c |   23 +++++++++++++++++++++--
 2 files changed, 26 insertions(+), 2 deletions(-)
drivers/mmc/core/core.c +5 -0
index 23f10f72e5f3..ca475a5c8d94 100644
@@ -900,6 +900,7 @@ int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
 
 	might_sleep();
 
+	pm_runtime_get_sync(&host->class_dev);
 	add_wait_queue(&host->wq, &wait);
 	spin_lock_irqsave(&host->lock, flags);
 	while (1) {
@@ -922,6 +923,8 @@ int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
 	remove_wait_queue(&host->wq, &wait);
 	if (host->ops->enable && !stop && host->claim_cnt == 1)
 		host->ops->enable(host);
+	if (stop)
+		pm_runtime_put(&host->class_dev);
 	return stop;
 }
 
@@ -953,6 +956,8 @@ void mmc_release_host(struct mmc_host *host)
 		spin_unlock_irqrestore(&host->lock, flags);
 		wake_up(&host->wq);
 	}
+	pm_runtime_mark_last_busy(&host->class_dev);
+	pm_runtime_put_autosuspend(&host->class_dev);
 }
 EXPORT_SYMBOL(mmc_release_host);
 
drivers/mmc/core/host.c +21 -2
index 8be0df758e68..c205c4531b44 100644
@@ -22,6 +22,7 @@
 #include <linux/leds.h>
 #include <linux/slab.h>
 #include <linux/suspend.h>
+#include <linux/pm_runtime.h>
 
 #include <linux/mmc/host.h>
 #include <linux/mmc/card.h>
@@ -490,6 +491,11 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 	host->class_dev.parent = dev;
 	host->class_dev.class = &mmc_host_class;
 	device_initialize(&host->class_dev);
+	if (dev)
+		/*
+		 * The device can sleep even when host is claimed.
+		 */
+		pm_suspend_ignore_children(dev, true);
 
 	if (mmc_gpio_alloc(host)) {
 		put_device(&host->class_dev);
@@ -532,15 +538,25 @@ EXPORT_SYMBOL(mmc_alloc_host);
 int mmc_add_host(struct mmc_host *host)
 {
 	int err;
+	struct device *dev = &host->class_dev;
 
 	WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
 		!host->ops->enable_sdio_irq);
 
-	err = device_add(&host->class_dev);
+	err = device_add(dev);
 	if (err)
 		return err;
+	pm_runtime_enable(dev);
+	/*
+	 * The host should be able to suspend while the attached card
+	 * stays awake.
+	 */
+	pm_suspend_ignore_children(dev, true);
+	pm_runtime_get_sync(dev);
+	pm_runtime_set_autosuspend_delay(dev, 100);
+	pm_runtime_use_autosuspend(dev);
 
-	led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
+	led_trigger_register_simple(dev_name(dev), &host->led);
 
 #ifdef CONFIG_DEBUG_FS
 	mmc_add_host_debugfs(host);
@@ -550,6 +566,9 @@ int mmc_add_host(struct mmc_host *host)
 	mmc_start_host(host);
 	register_pm_notifier(&host->pm_notify);
 
+	pm_runtime_mark_last_busy(dev);
+	pm_runtime_put_autosuspend(dev);
+
 	return 0;
 }
 
author
#4 lore raw

PATCH 3/4 mmc/sdio: keep device awake when interrupts expected in 4bit mode

In reply to NeilBrown

In 4-bit mode, SDIO interrupts are only reliable if clocks are maintained.
So get an extra reference when that is the case.

---
 drivers/mmc/core/sdio.c |    5 +++++
 1 file changed, 5 insertions(+)
drivers/mmc/core/sdio.c +5 -0
index 5bc6c7dbbd60..f432b11677bd 100644
@@ -227,6 +227,9 @@ static int sdio_enable_wide(struct mmc_card *card)
 	if (ret)
 		return ret;
 
+	/* Interrupts only work in 4bit mode if clocks stay on */
+	if (card->host->caps & MMC_CAP_SDIO_IRQ)
+		pm_runtime_get(card->host->parent);
 	return 1;
 }
 
@@ -283,6 +286,8 @@ static int sdio_disable_wide(struct mmc_card *card)
 		return ret;
 
 	mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
+	if (card->host->caps & MMC_CAP_SDIO_IRQ)
+		pm_runtime_put(card->host->parent);
 
 	return 0;
 }
author
#5 lore raw

PATCH 4/4 mmc/host: use runtime_pm to switch to 1-bit mode

In reply to NeilBrown

sdio card needs to switch to 1-bit more before the device can go to sleep. So when the host has been unclaimed for a while, do that using runtime_pm.

---
 drivers/mmc/core/host.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/core/sdio.c |    4 ++-
 2 files changed, 57 insertions(+), 2 deletions(-)
drivers/mmc/core/host.c +55 -0
index c205c4531b44..866df1858e6e 100644
@@ -47,9 +47,64 @@ static void mmc_host_classdev_release(struct device *dev)
 	kfree(host);
 }
 
+int sdio_disable_wide(struct mmc_card *card);
+
+static int mmc_host_runtime_suspend(struct device *dev)
+{
+	/* If SDIO card is in 4bit mode, we need to
+	 * switch it to 1-bit mode so that it can
+	 * go to sleep.
+	 */
+	struct mmc_host *host = cls_dev_to_mmc_host(dev);
+	if (host->card && host->card->type == MMC_TYPE_SDIO &&
+		host->ios.bus_width == 4) {
+		/* The host cannot be claimed while suspending,
+		 * so we are safe
+		 */
+		BUG_ON(host->claimed);
+		host->claimed = 1;
+		host->claim_cnt = 1;
+		host->claimer = current;
+		sdio_disable_wide(host->card);
+		host->claimed = 0;
+		host->claim_cnt = 0;
+		host->claimer = NULL;
+		host->unused = 1;
+	}
+	return 0;
+}
+
+int sdio_enable_wide(struct mmc_card *card);
+static int mmc_host_runtime_resume(struct device *dev)
+{
+	struct mmc_host *host = cls_dev_to_mmc_host(dev);
+	if (host->card && host->card->type == MMC_TYPE_SDIO &&
+		host->unused) {
+		/* The host cannot be claimed while suspending,
+		 * so we are safe
+		 */
+		BUG_ON(host->claimed);
+		host->claimed = 1;
+		host->claim_cnt = 1;
+		host->claimer = current;
+		sdio_enable_wide(host->card);
+		host->claimed = 0;
+		host->claim_cnt = 0;
+		host->claimer = NULL;
+		host->unused = 0;
+	}
+	return 0;
+}
+
+static struct dev_pm_ops mmc_host_dev_pm_ops = {
+	.runtime_suspend= mmc_host_runtime_suspend,
+	.runtime_resume	= mmc_host_runtime_resume,
+};
+
 static struct class mmc_host_class = {
 	.name		= "mmc_host",
 	.dev_release	= mmc_host_classdev_release,
+	.pm		= &mmc_host_dev_pm_ops,
 };
 
 int mmc_register_host_class(void)
drivers/mmc/core/sdio.c +2 -2
index f432b11677bd..b4713be32fb9 100644
@@ -200,7 +200,7 @@ out:
 	return ret;
 }
 
-static int sdio_enable_wide(struct mmc_card *card)
+int sdio_enable_wide(struct mmc_card *card)
 {
 	int ret;
 	u8 ctrl;
@@ -260,7 +260,7 @@ static int sdio_disable_cd(struct mmc_card *card)
  * Devices that remain active during a system suspend are
  * put back into 1-bit mode.
  */
-static int sdio_disable_wide(struct mmc_card *card)
+int sdio_disable_wide(struct mmc_card *card)
 {
 	int ret;
 	u8 ctrl;
#2 lore raw

Re: [PATCH 1/4] mmc: core: fold mmc_set_bus_width calls into sdio_enable_4bit_bus.

In reply to NeilBrown
Quoted 10 lines · NeilBrown

From: NeilBrown <[email protected]>

Every call to sdio_enable_4bit_bus is followed (on success) but a call to mmc_set_bus_width().

To simplify the code, include those calls directly in sdio_enable_4bit_bus().

This one is already applied. Thanks!

Kind regards
Uffe

Quoted 77 lines
---
 drivers/mmc/core/sdio.c |   32 ++++++++++++--------------------
 1 file changed, 12 insertions(+), 20 deletions(-)
drivers/mmc/core/sdio.c +12 -20
index ce6cc47206b0..5bc6c7dbbd60 100644
@@ -293,19 +293,22 @@ static int sdio_enable_4bit_bus(struct mmc_card *card)
        int err;
 
        if (card->type == MMC_TYPE_SDIO)
-               return sdio_enable_wide(card);
-
-       if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
-               (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
+               err = sdio_enable_wide(card);
+       else if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
+                (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
                err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
                if (err)
                        return err;
+               err = sdio_enable_wide(card);
+               if (err <= 0)
+                       mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
        } else
                return 0;
 
-       err = sdio_enable_wide(card);
-       if (err <= 0)
-               mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
+       if (err > 0) {
+               mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
+               err = 0;
+       }
 
        return err;
 }
@@ -547,13 +550,8 @@ static int mmc_sdio_init_uhs_card(struct mmc_card *card)
        /*
         * Switch to wider bus (if supported).
         */
-       if (card->host->caps & MMC_CAP_4_BIT_DATA) {
+       if (card->host->caps & MMC_CAP_4_BIT_DATA)
                err = sdio_enable_4bit_bus(card);
-               if (err > 0) {
-                       mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
-                       err = 0;
-               }
-       }
 
        /* Set the driver strength for the card */
        sdio_select_driver_type(card);
@@ -803,9 +801,7 @@ try_again:
                 * Switch to wider bus (if supported).
                 */
                err = sdio_enable_4bit_bus(card);
-               if (err > 0)
-                       mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
-               else if (err)
+               if (err)
                        goto remove;
        }
 finish:
@@ -983,10 +979,6 @@ static int mmc_sdio_resume(struct mmc_host *host)
        } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
                /* We may have switched to 1-bit mode during suspend */
                err = sdio_enable_4bit_bus(host->card);
-               if (err > 0) {
-                       mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
-                       err = 0;
-               }
        }
 
        if (!err && host->sdio_irqs) {