Skip to contentSkip to conversation

Add devfreq based clock scaling support for mmc

PATCH In review Sayali Lokhande 2 participants 9 messages Oct 8, 2018 – Oct 17, 2018

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

cover Add devfreq based clock scaling support for mmc

This change adds the use of devfreq based clock scaling to MMC. Both eMMC and SD card can use it.
For some workloads, such as video playback, it isn't necessary for these cards to run at high speed. Running at lower frequency, in such cases can still meet the deadlines for data transfers. Scaling down the clock frequency dynamically has power savings not only because the bus is running at lower frequency but also has an advantage of scaling down the system core voltage, if supported. Provide an ondemand clock scaling support similar to the cpufreq ondemand governor having two thresholds, up_threshold and down_threshold to decide whether to increase the frequency or scale it down respectively as per load.

Changes since RFC --> V1 :
mmc: core: devfreq: Add devfreq based clock scaling support Updated condition to check *freq passed to mmc_devfreq_set_target() with target_freq instead of checking with current freq.

Sahitya Tummala (1):
devfreq: Add new flag to do simple clock scaling

Sayali Lokhande (6):
mmc: core: devfreq: Add devfreq based clock scaling support
mmc: core: Add sysfs entries for dynamic control of clock scaling
mmc: core: add support for devfreq suspend/resume
mmc: sdhci-msm: Kconfig: select devfreq ondemand for sdhci-msm
mmc: sdhci-msm: Enable clock scaling property
mmc: core: Add a debugfs entry to set max clock rate

 .../devicetree/bindings/mmc/sdhci-msm.txt          |  10 +
 Documentation/mmc/mmc-dev-attrs.txt                |  38 ++
 drivers/devfreq/governor_simpleondemand.c          |  25 +-
 drivers/mmc/core/core.c                            | 668 +++++++++++++++++++++
 drivers/mmc/core/core.h                            |   9 +
 drivers/mmc/core/debugfs.c                         |  90 +++
 drivers/mmc/core/host.c                            | 163 ++++-
 drivers/mmc/core/mmc.c                             | 227 ++++++-
 drivers/mmc/core/sd.c                              |  85 ++-
 drivers/mmc/host/Kconfig                           |   2 +
 drivers/mmc/host/sdhci-msm.c                       |  38 ++
 drivers/mmc/host/sdhci-pltfm.c                     |  11 +
 drivers/mmc/host/sdhci.c                           |  27 +
 drivers/mmc/host/sdhci.h                           |   8 +
 include/linux/devfreq.h                            |   4 +
 include/linux/mmc/card.h                           |   5 +
 include/linux/mmc/host.h                           |  70 +++
 17 files changed, 1471 insertions(+), 9 deletions(-)
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw

PATCH 1/7 devfreq: Add new flag to do simple clock scaling

From: Sahitya Tummala <[email protected]>

Add new flag "simple_scaling" to on demand governor so that the clocks can be scaled up only when the load is more than up threshold and can be scaled down only when the load is less than down differential data as provided within struct devfreq_simple_ondemand_data.

---
 drivers/devfreq/governor_simpleondemand.c | 25 +++++++++++++++++++------
 include/linux/devfreq.h                   |  4 ++++
 2 files changed, 23 insertions(+), 6 deletions(-)
drivers/devfreq/governor_simpleondemand.c +19 -6
index 28e0f2d..532801b 100644
@@ -28,6 +28,7 @@ static int devfreq_simple_ondemand_func(struct devfreq *df,
 	unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
 	struct devfreq_simple_ondemand_data *data = df->data;
 	unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
+	unsigned long min = (df->min_freq) ? df->min_freq : 0;
 
 	err = devfreq_update_stats(df);
 	if (err)
@@ -45,18 +46,30 @@ static int devfreq_simple_ondemand_func(struct devfreq *df,
 	    dfso_upthreshold < dfso_downdifferential)
 		return -EINVAL;
 
-	/* Assume MAX if it is going to be divided by zero */
-	if (stat->total_time == 0) {
-		*freq = max;
-		return 0;
-	}
-
 	/* Prevent overflow */
 	if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
 		stat->busy_time >>= 7;
 		stat->total_time >>= 7;
 	}
 
+	if (data && data->simple_scaling) {
+		if (stat->busy_time * 100 >
+		    stat->total_time * dfso_upthreshold)
+			*freq = max;
+		else if (stat->busy_time * 100 <
+		    stat->total_time * dfso_downdifferential)
+			*freq = min;
+		else
+			*freq = df->previous_freq;
+		return 0;
+	}
+
+	/* Assume MAX if it is going to be divided by zero */
+	if (stat->total_time == 0) {
+		*freq = max;
+		return 0;
+	}
+
 	/* Set MAX if it's busy enough */
 	if (stat->busy_time * 100 >
 	    stat->total_time * dfso_upthreshold) {
include/linux/devfreq.h +4 -0
index 3aae5b3..2957fd8 100644
@@ -236,6 +236,9 @@ extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
  *			the governor may consider slowing the frequency down.
  *			Specify 0 to use the default. Valid value = 0 to 100.
  *			downdifferential < upthreshold must hold.
+ * @simple_scaling:	Setting this flag will scale the clocks up only if the
+ *			load is above @upthreshold and will scale the clocks
+ *			down only if the load is below @downdifferential.
  *
  * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
  * the governor uses the default values.
@@ -243,6 +246,7 @@ extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
 struct devfreq_simple_ondemand_data {
 	unsigned int upthreshold;
 	unsigned int downdifferential;
+	unsigned int simple_scaling;
 };
 #endif
 
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

PATCH 2/7 mmc: core: devfreq: Add devfreq based clock scaling support

In reply to Sayali Lokhande

This change adds the use of devfreq to MMC.
Both eMMC and SD card will use it.
For some workloads, such as video playback, it isn't necessary for these cards to run at high speed. Running at lower frequency, for example 52MHz, in such cases can still meet the deadlines for data transfers. Scaling down the clock frequency dynamically has power savings not only because the bus is running at lower frequency but also has an advantage of scaling down the system core voltage, if supported.
Provide an ondemand clock scaling support similar to the cpufreq ondemand governor having two thresholds, up_threshold and down_threshold to decide whether to increase the frequency or scale it down respectively. The sampling interval is in the order of milliseconds. If sampling interval is too low, frequent switching of frequencies can lead to high power consumption and if sampling interval is too high, the clock scaling logic would take long time to realize that the underlying hardware (controller and card) is busy and scale up the clocks.

---
 .../devicetree/bindings/mmc/sdhci-msm.txt          |  10 +
 drivers/mmc/core/core.c                            | 556 +++++++++++++++++++++
 drivers/mmc/core/core.h                            |   7 +
 drivers/mmc/core/debugfs.c                         |  46 ++
 drivers/mmc/core/host.c                            |   8 +
 drivers/mmc/core/mmc.c                             | 200 +++++++-
 drivers/mmc/core/sd.c                              |  72 ++-
 drivers/mmc/host/sdhci-msm.c                       |  37 ++
 drivers/mmc/host/sdhci-pltfm.c                     |  11 +
 drivers/mmc/host/sdhci.c                           |  27 +
 drivers/mmc/host/sdhci.h                           |   8 +
 include/linux/mmc/card.h                           |   5 +
 include/linux/mmc/host.h                           |  70 +++
 13 files changed, 1055 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/mmc/sdhci-msm.txt +10 -0
index 502b3b8..bd8470a 100644
@@ -26,6 +26,15 @@ Required properties:
 	"cal"	- reference clock for RCLK delay calibration (optional)
 	"sleep"	- sleep clock for RCLK delay calibration (optional)
 
+Optional Properties:
+- qcom,devfreq,freq-table - specifies supported frequencies for clock scaling.
+				    Clock scaling logic shall toggle between these frequencies based
+				    on card load. In case the defined frequencies are over or below
+				    the supported card frequencies, they will be overridden
+				    during card init. In case this entry is not supplied,
+				    the driver will construct one based on the card
+				    supported max and min frequencies.
+				    The frequencies must be ordered from lowest to highest.
 Example:
 
 	sdhc_1: sdhci@f9824900 {
@@ -43,6 +52,7 @@ Example:
 
 		clocks = <&gcc GCC_SDCC1_APPS_CLK>, <&gcc GCC_SDCC1_AHB_CLK>;
 		clock-names = "core", "iface";
+		qcom,devfreq,freq-table = <52000000 200000000>;
 	};
 
 	sdhc_2: sdhci@f98a4900 {
drivers/mmc/core/core.c +556 -0

580 lines not on this page · raw message

drivers/mmc/core/core.h +7 -0
index 9d8f09a..fc0a9b7 100644
@@ -34,6 +34,7 @@ struct mmc_bus_ops {
 	int (*shutdown)(struct mmc_host *);
 	int (*hw_reset)(struct mmc_host *);
 	int (*sw_reset)(struct mmc_host *);
+	int (*change_bus_speed)(struct mmc_host *, unsigned long *);
 };
 
 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
@@ -46,6 +47,8 @@ struct device_node *mmc_of_find_child_device(struct mmc_host *host,
 
 void mmc_set_chip_select(struct mmc_host *host, int mode);
 void mmc_set_clock(struct mmc_host *host, unsigned int hz);
+int mmc_clk_update_freq(struct mmc_host *host,
+		unsigned long freq, enum mmc_load state);
 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode);
 void mmc_set_bus_width(struct mmc_host *host, unsigned int width);
 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr);
@@ -91,6 +94,10 @@ static inline void mmc_delay(unsigned int ms)
 void mmc_add_card_debugfs(struct mmc_card *card);
 void mmc_remove_card_debugfs(struct mmc_card *card);
 
+extern bool mmc_can_scale_clk(struct mmc_host *host);
+extern int mmc_init_clk_scaling(struct mmc_host *host);
+extern int mmc_exit_clk_scaling(struct mmc_host *host);
+
 int mmc_execute_tuning(struct mmc_card *card);
 int mmc_hs200_to_hs400(struct mmc_card *card);
 int mmc_hs400_to_hs200(struct mmc_card *card);
drivers/mmc/core/debugfs.c +46 -0

58 lines not on this page · raw message

drivers/mmc/core/host.c +8 -0
index abf9e88..1e46aa4 100644
@@ -32,6 +32,10 @@
 #include "pwrseq.h"
 #include "sdio_ops.h"
 
+#define MMC_DEVFRQ_DEFAULT_UP_THRESHOLD 35
+#define MMC_DEVFRQ_DEFAULT_DOWN_THRESHOLD 5
+#define MMC_DEVFRQ_DEFAULT_POLLING_MSEC 100
+
 #define cls_dev_to_mmc_host(d)	container_of(d, struct mmc_host, class_dev)
 
 static DEFINE_IDA(mmc_host_ida);
@@ -435,6 +439,10 @@ int mmc_add_host(struct mmc_host *host)
 		return err;
 
 	led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
+	host->clk_scaling.upthreshold = MMC_DEVFRQ_DEFAULT_UP_THRESHOLD;
+	host->clk_scaling.downthreshold = MMC_DEVFRQ_DEFAULT_DOWN_THRESHOLD;
+	host->clk_scaling.polling_delay_ms = MMC_DEVFRQ_DEFAULT_POLLING_MSEC;
+	host->clk_scaling.skip_clk_scale_freq_update = false;
 
 #ifdef CONFIG_DEBUG_FS
 	mmc_add_host_debugfs(host);
drivers/mmc/core/mmc.c +199 -1

248 lines not on this page · raw message

drivers/mmc/core/sd.c +71 -1

108 lines not on this page · raw message

drivers/mmc/host/sdhci-msm.c +37 -0
index b5519a5..e9fe8c6 100644
@@ -1705,6 +1705,43 @@ static int sdhci_msm_register_vreg(struct sdhci_msm_host *msm_host)
 
 MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
 
+int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
+				 u32 **out, int *len, u32 size)
+{
+	int ret = 0;
+	struct device_node *np = dev->of_node;
+	size_t sz;
+	u32 *arr = NULL;
+
+	if (!of_get_property(np, prop_name, len)) {
+		ret = -EINVAL;
+		goto out;
+	}
+	sz = *len = *len / sizeof(*arr);
+	if (sz <= 0 || (size > 0 && (sz > size))) {
+		dev_err(dev, "%s invalid size\n", prop_name);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
+	if (!arr) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = of_property_read_u32_array(np, prop_name, arr, sz);
+	if (ret < 0) {
+		dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
+		goto out;
+	}
+	*out = arr;
+out:
+	if (ret)
+		*len = 0;
+	return ret;
+}
+
 static const struct sdhci_ops sdhci_msm_ops = {
 	.reset = sdhci_reset,
 	.set_clock = sdhci_msm_set_clock,
drivers/mmc/host/sdhci-pltfm.c +11 -0
index 02bea61..354fc68 100644
@@ -36,6 +36,9 @@
 #endif
 #include "sdhci-pltfm.h"
 
+int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
+				 u32 **out, int *len, u32 size);
+
 unsigned int sdhci_pltfm_clk_get_max_clock(struct sdhci_host *host)
 {
 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
@@ -101,6 +104,14 @@ void sdhci_get_of_property(struct platform_device *pdev)
 
 	of_property_read_u32(np, "clock-frequency", &pltfm_host->clock);
 
+	if (sdhci_msm_dt_get_array(&pdev->dev, "qcom,devfreq,freq-table",
+			&host->mmc->clk_scaling.pltfm_freq_table,
+			&host->mmc->clk_scaling.pltfm_freq_table_sz, 0))
+		pr_debug("no clock scaling frequencies were supplied\n");
+	else if (!host->mmc->clk_scaling.pltfm_freq_table ||
+			!host->mmc->clk_scaling.pltfm_freq_table_sz)
+		pr_err("bad dts clock scaling frequencies\n");
+
 	if (of_find_property(np, "keep-power-in-suspend", NULL))
 		host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
 
drivers/mmc/host/sdhci.c +27 -0
index 162b9af..f0aafab 100644
@@ -2427,6 +2427,32 @@ static void sdhci_card_event(struct mmc_host *mmc)
 	spin_unlock_irqrestore(&host->lock, flags);
 }
 
+static inline void sdhci_update_power_policy(struct sdhci_host *host,
+		enum sdhci_power_policy policy)
+{
+	host->power_policy = policy;
+}
+
+static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state)
+{
+	int err = 0;
+	struct sdhci_host *host = mmc_priv(mmc);
+
+	switch (state) {
+	case MMC_LOAD_HIGH:
+		sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE);
+		break;
+	case MMC_LOAD_LOW:
+		sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE);
+		break;
+	default:
+		err = -EINVAL;
+		break;
+	}
+
+	return err;
+}
+
 static const struct mmc_host_ops sdhci_ops = {
 	.request	= sdhci_request,
 	.post_req	= sdhci_post_req,
@@ -2441,6 +2467,7 @@ static void sdhci_card_event(struct mmc_host *mmc)
 	.execute_tuning			= sdhci_execute_tuning,
 	.card_event			= sdhci_card_event,
 	.card_busy	= sdhci_card_busy,
+	.notify_load = sdhci_notify_load,
 };
 
 /*****************************************************************************\
drivers/mmc/host/sdhci.h +8 -0
index 3b0c97a..740471f 100644
@@ -346,6 +346,12 @@ enum sdhci_cookie {
 	COOKIE_MAPPED,		/* mapped by sdhci_prepare_data() */
 };
 
+enum sdhci_power_policy {
+	SDHCI_PERFORMANCE_MODE,
+	SDHCI_POWER_SAVE_MODE,
+	SDHCI_POWER_POLICY_NUM /* Always keep this one last */
+};
+
 struct sdhci_host {
 	/* Data set by hardware interface driver */
 	const char *hw_name;	/* Hardware bus name */
@@ -562,6 +568,8 @@ struct sdhci_host {
 	/* Delay (ms) between tuning commands */
 	int			tuning_delay;
 
+	enum sdhci_power_policy power_policy;
+
 	/* Host SDMA buffer boundary. */
 	u32			sdma_boundary;
 
include/linux/mmc/card.h +5 -0
index de73778..c713581 100644
@@ -245,6 +245,10 @@ struct mmc_card {
 	struct mmc_host		*host;		/* the host this device belongs to */
 	struct device		dev;		/* the device */
 	u32			ocr;		/* the current OCR setting */
+	unsigned long		clk_scaling_lowest;	/* lowest scaleable*/
+							/* frequency */
+	unsigned long		clk_scaling_highest;	/* highest scaleable */
+
 	unsigned int		rca;		/* relative card address of device */
 	unsigned int		type;		/* card type */
 #define MMC_TYPE_MMC		0		/* MMC card */
@@ -308,6 +312,7 @@ struct mmc_card {
 	unsigned int    nr_parts;
 
 	unsigned int		bouncesz;	/* Bounce buffer size */
+	unsigned int		part_curr;
 };
 
 static inline bool mmc_large_sector(struct mmc_card *card)
include/linux/mmc/host.h +70 -0

109 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#4 lore raw

PATCH 3/7 mmc: core: Add sysfs entries for dynamic control of clock scaling

In reply to Sayali Lokhande

Add sysfs attributes to allow dynamic control of clock scaling parameters. These attributes are used to enable/disable clock scaling at runtime and change the up_threshold, down_threshold, and polling_interval values. Complete documentation for these sysfs entries are provided at "Documentation/mmc/mmc-dev-attrs.txt".

---
 Documentation/mmc/mmc-dev-attrs.txt |  38 +++++++++
 drivers/mmc/core/host.c             | 151 +++++++++++++++++++++++++++++++++++-
 2 files changed, 188 insertions(+), 1 deletion(-)
Documentation/mmc/mmc-dev-attrs.txt +38 -0
index 4ad0bb1..b02b2b4 100644
@@ -75,3 +75,41 @@ Note on raw_rpmb_size_mult:
 	"raw_rpmb_size_mult" is a multiple of 128kB block.
 	RPMB size in byte is calculated by using the following equation:
 	RPMB partition size = 128kB x raw_rpmb_size_mult
+
+SD/MMC Clock Scaling Attributes
+====================================
+
+Read and write accesses are provided to following attributes.
+
+	polling_interval	Measured in milliseconds, this attribute
+				defines how often we need to check the card
+				usage and make decisions on frequency scaling.
+
+	up_threshold		This attribute defines what should be the
+				average card usage between the polling
+				interval for the mmc core to make a decision
+				on whether it should increase the frequency.
+				For example when it is set to '35' it means
+				that between the checking intervals the card
+				needs to be on average more than 35% in use to
+				scale up the frequency. The value should be
+				between 0 - 100 so that it can be compared
+				against load percentage.
+
+	down_threshold		Similar to up_threshold, but on lowering the
+				frequency. For example, when it is set to '2'
+				it means that between the checking intervals
+				the card needs to be on average less than 2%
+				in use to scale down the clocks to minimum
+				frequency. The value should be between 0 - 100
+				so that it can be compared against load
+				percentage.
+
+	enable			Enable clock scaling for hosts (and cards)
+				that support ultrahigh speed modes
+				(SDR104, DDR50, HS200).
+
+echo <desired value> > /sys/class/mmc_host/mmcX/clk_scaling/polling_interval
+echo <desired value> > /sys/class/mmc_host/mmcX/clk_scaling/up_threshold
+echo <desired value> > /sys/class/mmc_host/mmcX/clk_scaling/down_threshold
+echo <desired value> > /sys/class/mmc_host/mmcX/clk_scaling/enable
drivers/mmc/core/host.c +150 -1

169 lines not on this page · raw message

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#5 lore raw

PATCH 4/7 mmc: core: add support for devfreq suspend/resume

In reply to Sayali Lokhande

This change adds support for devfreq suspend/resume to be called on each system suspend/resume, runtime suspend/resume, power restore.

---
 drivers/mmc/core/core.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/core/core.h |   2 +
 drivers/mmc/core/host.c |   8 +++-
 drivers/mmc/core/mmc.c  |  27 ++++++++++++
 drivers/mmc/core/sd.c   |  13 ++++++
 5 files changed, 160 insertions(+), 2 deletions(-)
drivers/mmc/core/core.c +112 -0
index a3d4a92..78be523 100644
@@ -614,6 +614,111 @@ int mmc_init_clk_scaling(struct mmc_host *host)
 EXPORT_SYMBOL(mmc_init_clk_scaling);
 
 /**
+ * mmc_suspend_clk_scaling() - suspend clock scaling
+ * @host: pointer to mmc host structure
+ *
+ * This API will suspend devfreq feature for the specific host.
+ * The statistics collected by mmc will be cleared.
+ * This function is intended to be called by the pm callbacks
+ * (e.g. runtime_suspend, suspend) of the mmc device
+ */
+int mmc_suspend_clk_scaling(struct mmc_host *host)
+{
+	int err;
+
+	if (!host) {
+		WARN(1, "bad host parameter\n");
+		return -EINVAL;
+	}
+
+	if (!mmc_can_scale_clk(host) || !host->clk_scaling.enable)
+		return 0;
+
+	if (!host->clk_scaling.devfreq) {
+		pr_err("%s: %s: no devfreq is assosiated with this device\n",
+			mmc_hostname(host), __func__);
+		return -EPERM;
+	}
+
+	atomic_inc(&host->clk_scaling.devfreq_abort);
+	wake_up(&host->wq);
+	err = devfreq_suspend_device(host->clk_scaling.devfreq);
+	if (err) {
+		pr_err("%s: %s: failed to suspend devfreq\n",
+			mmc_hostname(host), __func__);
+		return err;
+	}
+	host->clk_scaling.enable = false;
+
+	host->clk_scaling.total_busy_time_us = 0;
+
+	pr_debug("%s: devfreq was removed\n", mmc_hostname(host));
+
+	return 0;
+}
+EXPORT_SYMBOL(mmc_suspend_clk_scaling);
+
+/**
+ * mmc_resume_clk_scaling() - resume clock scaling
+ * @host: pointer to mmc host structure
+ *
+ * This API will resume devfreq feature for the specific host.
+ * This API is intended to be called by the pm callbacks
+ * (e.g. runtime_suspend, suspend) of the mmc device
+ */
+int mmc_resume_clk_scaling(struct mmc_host *host)
+{
+	int err = 0;
+	u32 max_clk_idx = 0;
+	u32 devfreq_max_clk = 0;
+	u32 devfreq_min_clk = 0;
+
+	if (!host) {
+		WARN(1, "bad host parameter\n");
+		return -EINVAL;
+	}
+
+	if (!mmc_can_scale_clk(host))
+		return 0;
+
+	/*
+	 * If clock scaling is already exited when resume is called, like
+	 * during mmc shutdown, it is not an error and should not fail the
+	 * API calling this.
+	 */
+	if (!host->clk_scaling.devfreq) {
+		pr_warn("%s: %s: no devfreq is assosiated with this device\n",
+			mmc_hostname(host), __func__);
+		return 0;
+	}
+
+	atomic_set(&host->clk_scaling.devfreq_abort, 0);
+
+	max_clk_idx = host->clk_scaling.freq_table_sz - 1;
+	devfreq_max_clk = host->clk_scaling.freq_table[max_clk_idx];
+	devfreq_min_clk = host->clk_scaling.freq_table[0];
+
+	host->clk_scaling.curr_freq = devfreq_max_clk;
+	if (host->ios.clock < host->clk_scaling.freq_table[max_clk_idx])
+		host->clk_scaling.curr_freq = devfreq_min_clk;
+
+	host->clk_scaling.clk_scaling_in_progress = false;
+	host->clk_scaling.need_freq_change = false;
+
+	err = devfreq_resume_device(host->clk_scaling.devfreq);
+	if (err) {
+		pr_err("%s: %s: failed to resume devfreq (%d)\n",
+			mmc_hostname(host), __func__, err);
+	} else {
+		host->clk_scaling.enable = true;
+		pr_debug("%s: devfreq resumed\n", mmc_hostname(host));
+	}
+
+	return err;
+}
+EXPORT_SYMBOL(mmc_resume_clk_scaling);
+
+/**
  * mmc_exit_devfreq_clk_scaling() - Disable clock scaling
  * @host: pointer to mmc host structure
  *
@@ -638,6 +743,13 @@ int mmc_exit_clk_scaling(struct mmc_host *host)
 		return -EPERM;
 	}
 
+	err = mmc_suspend_clk_scaling(host);
+	if (err) {
+		pr_err("%s: %s: fail to suspend clock scaling (%d)\n",
+			mmc_hostname(host), __func__,  err);
+		return err;
+	}
+
 	err = devfreq_remove_device(host->clk_scaling.devfreq);
 	if (err) {
 		pr_err("%s: remove devfreq failed (%d)\n",
drivers/mmc/core/core.h +2 -0
index fc0a9b7..249c20d 100644
@@ -97,6 +97,8 @@ static inline void mmc_delay(unsigned int ms)
 extern bool mmc_can_scale_clk(struct mmc_host *host);
 extern int mmc_init_clk_scaling(struct mmc_host *host);
 extern int mmc_exit_clk_scaling(struct mmc_host *host);
+extern int mmc_suspend_clk_scaling(struct mmc_host *host);
+extern int mmc_resume_clk_scaling(struct mmc_host *host);
 
 int mmc_execute_tuning(struct mmc_card *card);
 int mmc_hs200_to_hs400(struct mmc_card *card);
drivers/mmc/core/host.c +6 -2
index 0504610..c3a71a5 100644
@@ -442,15 +442,19 @@ static ssize_t store_enable(struct device *dev,
 	mmc_get_card(host->card, NULL);
 
 	if (!value) {
-		/* mask host capability */
+		/* Suspend the clock scaling and mask host capability */
+		if (host->clk_scaling.enable)
+			mmc_suspend_clk_scaling(host);
 		host->caps2 &= ~MMC_CAP2_CLK_SCALE;
 		host->clk_scaling.state = MMC_LOAD_HIGH;
 		/* Set to max. frequency when disabling */
 		mmc_clk_update_freq(host, host->card->clk_scaling_highest,
 					host->clk_scaling.state);
 	} else if (value) {
-		/* Unmask host capability*/
+		/* Unmask host capability and resume scaling */
 		host->caps2 |= MMC_CAP2_CLK_SCALE;
+		if (!host->clk_scaling.enable)
+			mmc_resume_clk_scaling(host);
 	}
 
 	mmc_put_card(host->card, NULL);
drivers/mmc/core/mmc.c +27 -0
index c8aedf3..1d286af 100644
@@ -2161,6 +2161,13 @@ static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
 	unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
 					EXT_CSD_POWER_OFF_LONG;
 
+	err = mmc_suspend_clk_scaling(host);
+	if (err) {
+		pr_err("%s: %s: fail to suspend clock scaling (%d)\n",
+			mmc_hostname(host), __func__, err);
+		return err;
+	}
+
 	mmc_claim_host(host);
 
 	if (mmc_card_suspended(host->card))
@@ -2190,6 +2197,8 @@ static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
 	}
 out:
 	mmc_release_host(host);
+	if (err)
+		mmc_resume_clk_scaling(host);
 	return err;
 }
 
@@ -2228,6 +2237,10 @@ static int _mmc_resume(struct mmc_host *host)
 
 out:
 	mmc_release_host(host);
+	err = mmc_resume_clk_scaling(host);
+	if (err)
+		pr_err("%s: %s: fail to resume clock scaling (%d)\n",
+			mmc_hostname(host), __func__, err);
 	return err;
 }
 
@@ -2335,6 +2348,15 @@ static int _mmc_hw_reset(struct mmc_host *host)
 		mmc_pwrseq_reset(host);
 	}
 
+	/* Suspend clk scaling to avoid switching frequencies intermittently */
+
+	ret = mmc_suspend_clk_scaling(host);
+	if (ret) {
+		pr_err("%s: %s: fail to suspend clock scaling (%d)\n",
+			mmc_hostname(host), __func__, ret);
+		return ret;
+	}
+
 	ret = mmc_init_card(host, card->ocr, card);
 	if (ret) {
 		pr_err("%s: %s: mmc_init_card failed (%d)\n",
@@ -2342,6 +2364,11 @@ static int _mmc_hw_reset(struct mmc_host *host)
 		return ret;
 	}
 
+	ret = mmc_resume_clk_scaling(host);
+	if (ret)
+		pr_err("%s: %s: fail to resume clock scaling (%d)\n",
+			mmc_hostname(host), __func__, ret);
+
 	return ret;
 }
 
drivers/mmc/core/sd.c +13 -0
index 40144c1..5ae2916 100644
@@ -1131,6 +1131,13 @@ static int _mmc_sd_suspend(struct mmc_host *host)
 {
 	int err = 0;
 
+	err = mmc_suspend_clk_scaling(host);
+	if (err) {
+		pr_err("%s: %s: fail to suspend clock scaling (%d)\n",
+			mmc_hostname(host), __func__,  err);
+		return err;
+	}
+
 	mmc_claim_host(host);
 
 	if (mmc_card_suspended(host->card))
@@ -1182,6 +1189,12 @@ static int _mmc_sd_resume(struct mmc_host *host)
 	err = mmc_sd_init_card(host, host->card->ocr, host->card);
 	mmc_card_clr_suspended(host->card);
 
+	err = mmc_resume_clk_scaling(host);
+	if (err) {
+		pr_err("%s: %s: fail to resume clock scaling (%d)\n",
+			mmc_hostname(host), __func__, err);
+		goto out;
+	}
 out:
 	mmc_release_host(host);
 	return err;
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#6 lore raw

PATCH 5/7 mmc: sdhci-msm: Kconfig: select devfreq ondemand for sdhci-msm

In reply to Sayali Lokhande

SDHCI-MSM platform is using devfreq ondemand governor. Select devfreq governor for SDHCI-MSM platform.

---
 drivers/mmc/host/Kconfig | 2 ++
 1 file changed, 2 insertions(+)
drivers/mmc/host/Kconfig +2 -0
index 0581c19..2ba2c62 100644
@@ -442,6 +442,8 @@ config MMC_SDHCI_MSM
 	tristate "Qualcomm SDHCI Controller Support"
 	depends on ARCH_QCOM || (ARM && COMPILE_TEST)
 	depends on MMC_SDHCI_PLTFM
+	select PM_DEVFREQ
+	select DEVFREQ_GOV_SIMPLE_ONDEMAND
 	select MMC_SDHCI_IO_ACCESSORS
 	help
 	  This selects the Secure Digital Host Controller Interface (SDHCI)
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#7 lore raw

PATCH 6/7 mmc: sdhci-msm: Enable clock scaling property

In reply to Sayali Lokhande

This change enables clock scaling capability for sdhci-msm platform driver.

---
 drivers/mmc/host/sdhci-msm.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mmc/host/sdhci-msm.c +1 -0
index e9fe8c6..8846a76 100644
@@ -1803,6 +1803,7 @@ static int sdhci_msm_probe(struct platform_device *pdev)
 	msm_offset = msm_host->offset;
 
 	sdhci_get_of_property(pdev);
+	msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
 
 	msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
 
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#8 lore raw

PATCH 7/7 mmc: core: Add a debugfs entry to set max clock rate

In reply to Sayali Lokhande

Limiting the max frequency supported by host controller to a certain value can be useful for testing power consumption at various frequencies that are supported by the host.
Note: If the card supports less than desired value then the frequency of operation would be limited to that frequency. Usage:
mount -t debugfs none /sys/kernel/debug
echo <desired_frequency> > /sys/kernel/debug/mmcX/max_clock cat /sys/kernel/debug/mmcX/max_clock

---
 drivers/mmc/core/debugfs.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
drivers/mmc/core/debugfs.c +44 -0
index 630ca8e..26499f7 100644
@@ -262,6 +262,46 @@ static int mmc_scale_set(void *data, u64 val)
 DEFINE_SIMPLE_ATTRIBUTE(mmc_scale_fops, mmc_scale_get, mmc_scale_set,
 	"%llu\n");
 
+static int mmc_max_clock_get(void *data, u64 *val)
+{
+	struct mmc_host *host = data;
+
+	if (!host)
+		return -EINVAL;
+
+	*val = host->f_max;
+
+	return 0;
+}
+
+static int mmc_max_clock_set(void *data, u64 val)
+{
+	struct mmc_host *host = data;
+	int err = -EINVAL;
+	unsigned long freq = val;
+	unsigned int old_freq;
+
+	if (!host || (val < host->f_min))
+		goto out;
+
+	mmc_claim_host(host);
+	if (host->bus_ops && host->bus_ops->change_bus_speed) {
+		old_freq = host->f_max;
+		host->f_max = freq;
+
+		err = host->bus_ops->change_bus_speed(host, &freq);
+
+		if (err)
+			host->f_max = old_freq;
+	}
+	mmc_release_host(host);
+out:
+	return err;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(mmc_max_clock_fops, mmc_max_clock_get,
+		mmc_max_clock_set, "%llu\n");
+
 void mmc_add_host_debugfs(struct mmc_host *host)
 {
 	struct dentry *root;
@@ -290,6 +330,10 @@ void mmc_add_host_debugfs(struct mmc_host *host)
 			&mmc_clock_fops))
 		goto err_node;
 
+	if (!debugfs_create_file("max_clock", 0600, root, host,
+		&mmc_max_clock_fops))
+		goto err_node;
+
 	if (!debugfs_create_file("scale", 0600, root, host,
 		&mmc_scale_fops))
 		goto err_node;
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
maintainer
#3 lore raw

Re: [PATCH V1 2/7] mmc: core: devfreq: Add devfreq based clock scaling support

In reply to Sayali Lokhande
Quoted 10 lines · Sayali Lokhande

This change adds the use of devfreq to MMC.
Both eMMC and SD card will use it.
For some workloads, such as video playback, it isn't necessary for these cards to run at high speed. Running at lower frequency, for example 52MHz, in such cases can still meet the deadlines for data transfers. Scaling down the clock frequency dynamically has power savings not only because the bus is running at lower frequency but also has an advantage of scaling down the system core voltage, if supported.

Is there really power savings if there's no voltage control?

Quoted 11 lines

Provide an ondemand clock scaling support similar to the cpufreq ondemand governor having two thresholds, up_threshold and down_threshold to decide whether to increase the frequency or scale it down respectively. The sampling interval is in the order of milliseconds. If sampling interval is too low, frequent switching of frequencies can lead to high power consumption and if sampling interval is too high, the clock scaling logic would take long time to realize that the underlying hardware (controller and card) is busy and scale up the clocks.

Why the short lines?

Quoted 5 lines
---
 .../devicetree/bindings/mmc/sdhci-msm.txt          |  10 +

Bindings should be separate patches.

Quoted 21 lines
 drivers/mmc/core/core.c                            | 556 +++++++++++++++++++++
 drivers/mmc/core/core.h                            |   7 +
 drivers/mmc/core/debugfs.c                         |  46 ++
 drivers/mmc/core/host.c                            |   8 +
 drivers/mmc/core/mmc.c                             | 200 +++++++-
 drivers/mmc/core/sd.c                              |  72 ++-
 drivers/mmc/host/sdhci-msm.c                       |  37 ++
 drivers/mmc/host/sdhci-pltfm.c                     |  11 +
 drivers/mmc/host/sdhci.c                           |  27 +
 drivers/mmc/host/sdhci.h                           |   8 +
 include/linux/mmc/card.h                           |   5 +
 include/linux/mmc/host.h                           |  70 +++
 13 files changed, 1055 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/mmc/sdhci-msm.txt +0 -0
index 502b3b8..bd8470a 100644
@@ -26,6 +26,15 @@ Required properties:
 	"cal"	- reference clock for RCLK delay calibration (optional)
 	"sleep"	- sleep clock for RCLK delay calibration (optional)

Documentation/devicetree/bindings/mmc/sdhci-msm.txt Required properties: · line 37

Quoted 10 lines
Documentation/devicetree/bindings/mmc/sdhci-msm.txt +9 -0
 
+Optional Properties:
+- qcom,devfreq,freq-table - specifies supported frequencies for clock scaling.
+				    Clock scaling logic shall toggle between these frequencies based
+				    on card load. In case the defined frequencies are over or below
+				    the supported card frequencies, they will be overridden
+				    during card init. In case this entry is not supplied,
+				    the driver will construct one based on the card
+				    supported max and min frequencies.
+				    The frequencies must be ordered from lowest to highest.

Why is this qcom specific?

I believe I also saw interconnect binding for SD/MMC. How does that relate to this? There should be some coordination of this work.

Quoted 11 lines
Documentation/devicetree/bindings/mmc/sdhci-msm.txt +1 -0
 Example:
 
 	sdhc_1: sdhci@f9824900 {
@@ -43,6 +52,7 @@ Example:
 
 		clocks = <&gcc GCC_SDCC1_APPS_CLK>, <&gcc GCC_SDCC1_AHB_CLK>;
 		clock-names = "core", "iface";
+		qcom,devfreq,freq-table = <52000000 200000000>;
 	};
 
 	sdhc_2: sdhci@f98a4900 {