2023-09-29 09:06:30

by Vincent Whitchurch

[permalink] [raw]
Subject: [PATCH v2 0/2] mmc: Allow speed modes to be adjusted dynamically

During board verification, there is a need to test the various supported
eMMC/SD speed modes. However, since the framework chooses the best mode
supported by the card and the host controller's caps, this currently
necessitates changing the devicetree for every iteration. This series
provides a way to adjust speed modes dynamically via debugfs.

--
Changes in v2:
- Replace module parameter with a debugfs file.
- Add patch to move mmc_select_card_type().
- Link to v1: https://lore.kernel.org/r/[email protected]/

---
Vincent Whitchurch (2):
mmc: core: Always reselect card type
mmc: debugfs: Allow host caps to be modified

drivers/mmc/core/debugfs.c | 51 ++++++++++++++++++++++++++++++++++++++++++++--
drivers/mmc/core/mmc.c | 7 ++++++-
2 files changed, 55 insertions(+), 3 deletions(-)
---
base-commit: 6465e260f48790807eef06b583b38ca9789b6072
change-id: 20230928-mmc-caps-2cc7c3295b2a

Best regards,
--
Vincent Whitchurch <[email protected]>


2023-09-29 12:48:19

by Vincent Whitchurch

[permalink] [raw]
Subject: [PATCH v2 1/2] mmc: core: Always reselect card type

We want to allow host caps to be changed dynamically via debugfs, so for
these to have an effect, ensure that the card type reselection is always
applied even if the card is old.

Signed-off-by: Vincent Whitchurch <[email protected]>
---
drivers/mmc/core/mmc.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 89cd48fcec79..d372e2098ffc 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -419,7 +419,6 @@ static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)

card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
- mmc_select_card_type(card);

card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
card->ext_csd.raw_erase_timeout_mult =
@@ -1732,6 +1731,12 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
mmc_set_erase_size(card);
}

+ /*
+ * Reselect the card type since host caps could have been changed when
+ * debugging even if the card is not new.
+ */
+ mmc_select_card_type(card);
+
/* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */
if (card->ext_csd.rev >= 3) {
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,

--
2.34.1

2023-09-29 16:00:24

by Vincent Whitchurch

[permalink] [raw]
Subject: [PATCH v2 2/2] mmc: debugfs: Allow host caps to be modified

During board verification, there is a need to test the various supported
eMMC/SD speed modes. However, since the framework chooses the best mode
supported by the card and the host controller's caps, this currently
necessitates changing the devicetree for every iteration.

Allow the various speed mode host capabilities to be modified via
debugfs in order to allow easier hardware verification. The values to
be written are the raw MMC_CAP* values from include/linux/mmc/host.h.
This is rather low-level, and these defines are not guaranteed to be
stable, but it is perhaps good enough for the intended use case.

MMC_CAP_AGGRESSIVE_PM can also be set, in order to be able to
re-initialize the card without having to physically remove and re-insert
it.

/sys/kernel/debug/mmc0# grep timing ios
timing spec: 9 (mmc HS200)

// Turn on MMC_CAP_AGGRESSIVE_PM and re-trigger runtime suspend
/sys/kernel/debug/mmc0# echo $(($(cat caps) | (1 << 7))) > caps
/sys/kernel/debug/mmc0# echo on > /sys/bus/mmc/devices/mmc0\:0001/power/control
/sys/kernel/debug/mmc0# echo auto > /sys/bus/mmc/devices/mmc0\:0001/power/control

// MMC_CAP2_HS200_1_8V_SDR
/sys/kernel/debug/mmc0# echo $(($(cat caps2) & ~(1 << 5))) > caps2
/sys/kernel/debug/mmc0# echo on > /sys/bus/mmc/devices/mmc0\:0001/power/control
/sys/kernel/debug/mmc0# grep timing ios
timing spec: 8 (mmc DDR52)

Signed-off-by: Vincent Whitchurch <[email protected]>
---
drivers/mmc/core/debugfs.c | 51 ++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/drivers/mmc/core/debugfs.c b/drivers/mmc/core/debugfs.c
index 2c97b94aab23..1642ea72d22c 100644
--- a/drivers/mmc/core/debugfs.c
+++ b/drivers/mmc/core/debugfs.c
@@ -12,9 +12,12 @@
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/fault-inject.h>
+#include <linux/time.h>

#include <linux/mmc/card.h>
#include <linux/mmc/host.h>
+#include <linux/mmc/mmc.h>
+#include <linux/mmc/sd.h>

#include "core.h"
#include "card.h"
@@ -298,6 +301,49 @@ static const struct file_operations mmc_err_stats_fops = {
.release = single_release,
};

+static int mmc_caps_get(void *data, u64 *val)
+{
+ *val = *(u32 *)data;
+ return 0;
+}
+
+static int mmc_caps_set(void *data, u64 val)
+{
+ u32 *caps = data;
+ u32 diff = *caps ^ val;
+ u32 allowed = MMC_CAP_AGGRESSIVE_PM |
+ MMC_CAP_SD_HIGHSPEED |
+ MMC_CAP_MMC_HIGHSPEED |
+ MMC_CAP_UHS |
+ MMC_CAP_DDR;
+
+ if (diff & ~allowed)
+ return -EINVAL;
+
+ *caps = val;
+
+ return 0;
+}
+
+static int mmc_caps2_set(void *data, u64 val)
+{
+ u32 allowed = MMC_CAP2_HSX00_1_8V | MMC_CAP2_HSX00_1_2V;
+ u32 *caps = data;
+ u32 diff = *caps ^ val;
+
+ if (diff & ~allowed)
+ return -EINVAL;
+
+ *caps = val;
+
+ return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(mmc_caps_fops, mmc_caps_get, mmc_caps_set,
+ "0x%08llx\n");
+DEFINE_DEBUGFS_ATTRIBUTE(mmc_caps2_fops, mmc_caps_get, mmc_caps2_set,
+ "0x%08llx\n");
+
void mmc_add_host_debugfs(struct mmc_host *host)
{
struct dentry *root;
@@ -306,8 +352,9 @@ void mmc_add_host_debugfs(struct mmc_host *host)
host->debugfs_root = root;

debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops);
- debugfs_create_x32("caps", S_IRUSR, root, &host->caps);
- debugfs_create_x32("caps2", S_IRUSR, root, &host->caps2);
+ debugfs_create_file("caps", 0600, root, &host->caps, &mmc_caps_fops);
+ debugfs_create_file("caps2", 0600, root, &host->caps2,
+ &mmc_caps2_fops);
debugfs_create_file_unsafe("clock", S_IRUSR | S_IWUSR, root, host,
&mmc_clock_fops);


--
2.34.1

2023-10-07 02:28:06

by Wenchao Chen

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] mmc: debugfs: Allow host caps to be modified

On Fri, 29 Sept 2023 at 21:17, Vincent Whitchurch
<[email protected]> wrote:
>
> During board verification, there is a need to test the various supported
> eMMC/SD speed modes. However, since the framework chooses the best mode
> supported by the card and the host controller's caps, this currently
> necessitates changing the devicetree for every iteration.
>
> Allow the various speed mode host capabilities to be modified via
> debugfs in order to allow easier hardware verification. The values to
> be written are the raw MMC_CAP* values from include/linux/mmc/host.h.
> This is rather low-level, and these defines are not guaranteed to be
> stable, but it is perhaps good enough for the intended use case.
>
> MMC_CAP_AGGRESSIVE_PM can also be set, in order to be able to
> re-initialize the card without having to physically remove and re-insert
> it.
>
> /sys/kernel/debug/mmc0# grep timing ios
> timing spec: 9 (mmc HS200)
>
> // Turn on MMC_CAP_AGGRESSIVE_PM and re-trigger runtime suspend
> /sys/kernel/debug/mmc0# echo $(($(cat caps) | (1 << 7))) > caps
> /sys/kernel/debug/mmc0# echo on > /sys/bus/mmc/devices/mmc0\:0001/power/control
> /sys/kernel/debug/mmc0# echo auto > /sys/bus/mmc/devices/mmc0\:0001/power/control
>
> // MMC_CAP2_HS200_1_8V_SDR
> /sys/kernel/debug/mmc0# echo $(($(cat caps2) & ~(1 << 5))) > caps2

$(($(cat caps2) & ~(1 << 5))) looks complicated, does it use echo DDR52 > caps2?

> /sys/kernel/debug/mmc0# echo on > /sys/bus/mmc/devices/mmc0\:0001/power/control
> /sys/kernel/debug/mmc0# grep timing ios
> timing spec: 8 (mmc DDR52)
>
> Signed-off-by: Vincent Whitchurch <[email protected]>
> ---
> drivers/mmc/core/debugfs.c | 51 ++++++++++++++++++++++++++++++++++++++++++++--
> 1 file changed, 49 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/mmc/core/debugfs.c b/drivers/mmc/core/debugfs.c
> index 2c97b94aab23..1642ea72d22c 100644
> --- a/drivers/mmc/core/debugfs.c
> +++ b/drivers/mmc/core/debugfs.c
> @@ -12,9 +12,12 @@
> #include <linux/slab.h>
> #include <linux/stat.h>
> #include <linux/fault-inject.h>
> +#include <linux/time.h>
>
> #include <linux/mmc/card.h>
> #include <linux/mmc/host.h>
> +#include <linux/mmc/mmc.h>
> +#include <linux/mmc/sd.h>
>
> #include "core.h"
> #include "card.h"
> @@ -298,6 +301,49 @@ static const struct file_operations mmc_err_stats_fops = {
> .release = single_release,
> };
>
> +static int mmc_caps_get(void *data, u64 *val)
> +{
> + *val = *(u32 *)data;
> + return 0;
> +}
> +
> +static int mmc_caps_set(void *data, u64 val)
> +{
> + u32 *caps = data;
> + u32 diff = *caps ^ val;
> + u32 allowed = MMC_CAP_AGGRESSIVE_PM |
> + MMC_CAP_SD_HIGHSPEED |
> + MMC_CAP_MMC_HIGHSPEED |
> + MMC_CAP_UHS |
> + MMC_CAP_DDR;
> +
> + if (diff & ~allowed)
> + return -EINVAL;
> +
> + *caps = val;
> +
> + return 0;
> +}
> +
> +static int mmc_caps2_set(void *data, u64 val)
> +{
> + u32 allowed = MMC_CAP2_HSX00_1_8V | MMC_CAP2_HSX00_1_2V;
> + u32 *caps = data;
> + u32 diff = *caps ^ val;
> +
> + if (diff & ~allowed)
> + return -EINVAL;
> +
> + *caps = val;
> +
> + return 0;
> +}
> +
> +DEFINE_DEBUGFS_ATTRIBUTE(mmc_caps_fops, mmc_caps_get, mmc_caps_set,
> + "0x%08llx\n");
> +DEFINE_DEBUGFS_ATTRIBUTE(mmc_caps2_fops, mmc_caps_get, mmc_caps2_set,
> + "0x%08llx\n");
> +
> void mmc_add_host_debugfs(struct mmc_host *host)
> {
> struct dentry *root;
> @@ -306,8 +352,9 @@ void mmc_add_host_debugfs(struct mmc_host *host)
> host->debugfs_root = root;
>
> debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops);
> - debugfs_create_x32("caps", S_IRUSR, root, &host->caps);
> - debugfs_create_x32("caps2", S_IRUSR, root, &host->caps2);
> + debugfs_create_file("caps", 0600, root, &host->caps, &mmc_caps_fops);
> + debugfs_create_file("caps2", 0600, root, &host->caps2,
> + &mmc_caps2_fops);

Would it be better to use "S_IRUSR | S_IWUSR" instead of "0600"?

> debugfs_create_file_unsafe("clock", S_IRUSR | S_IWUSR, root, host,
> &mmc_clock_fops);
>
>
> --
> 2.34.1
>

2023-10-09 07:28:25

by Vincent Whitchurch

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] mmc: debugfs: Allow host caps to be modified

On Sat, 2023-10-07 at 10:27 +0800, Wenchao Chen wrote:
> On Fri, 29 Sept 2023 at 21:17, Vincent Whitchurch
> <[email protected]> wrote:
> >  // MMC_CAP2_HS200_1_8V_SDR
> >  /sys/kernel/debug/mmc0# echo $(($(cat caps2) & ~(1 << 5))) > caps2
>
> $(($(cat caps2) & ~(1 << 5))) looks complicated, does it use echo DDR52 > caps2?

1 << 5 is (as the comment above says) MMC_CAP2_HS200_1_8V_SDR. The
read-modify-write is needed to not clear unrelated bits. The MMC
framework picks the best possible mode supported by both the card and
the host controller, so disabling support for HS200 in the host
controller leads to DDR52 being picked in this case.

[...]
> >  void mmc_add_host_debugfs(struct mmc_host *host)
> >  {
> >         struct dentry *root;
> > @@ -306,8 +352,9 @@ void mmc_add_host_debugfs(struct mmc_host *host)
> >         host->debugfs_root = root;
> >
> >         debugfs_create_file("ios", S_IRUSR, root, host, &mmc_ios_fops);
> > - debugfs_create_x32("caps", S_IRUSR, root, &host->caps);
> > - debugfs_create_x32("caps2", S_IRUSR, root, &host->caps2);
> > + debugfs_create_file("caps", 0600, root, &host->caps, &mmc_caps_fops);
> > + debugfs_create_file("caps2", 0600, root, &host->caps2,
> > + &mmc_caps2_fops);
>
> Would it be better to use "S_IRUSR | S_IWUSR" instead of "0600"?

No, not according to checkpatch which says that numeric permissions are
preferred.

2023-10-10 14:28:42

by Ulf Hansson

[permalink] [raw]
Subject: Re: [PATCH v2 0/2] mmc: Allow speed modes to be adjusted dynamically

On Fri, 29 Sept 2023 at 09:45, Vincent Whitchurch
<[email protected]> wrote:
>
> During board verification, there is a need to test the various supported
> eMMC/SD speed modes. However, since the framework chooses the best mode
> supported by the card and the host controller's caps, this currently
> necessitates changing the devicetree for every iteration. This series
> provides a way to adjust speed modes dynamically via debugfs.
>
> --
> Changes in v2:
> - Replace module parameter with a debugfs file.
> - Add patch to move mmc_select_card_type().
> - Link to v1: https://lore.kernel.org/r/[email protected]/
>
> ---
> Vincent Whitchurch (2):
> mmc: core: Always reselect card type
> mmc: debugfs: Allow host caps to be modified
>
> drivers/mmc/core/debugfs.c | 51 ++++++++++++++++++++++++++++++++++++++++++++--
> drivers/mmc/core/mmc.c | 7 ++++++-
> 2 files changed, 55 insertions(+), 3 deletions(-)
> ---

Applied for next, thanks!

Kind regards
Uffe