1 From d8748eda96b47efea833e2a6ea478bb370654f1d Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.com>
3 Date: Wed, 20 Mar 2024 13:00:30 +0000
4 Subject: [PATCH 0983/1085] drivers: mmc: preallocate a block for SD extension
7 The Performance Extension register is regularly accessed in a hot path
8 to do write cache flushes. Don't invoke kmalloc/kfree for every access,
9 preallocate a 512B buffer for this purpose.
11 Also remove an unused alloc in sd_enable_cache().
13 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
15 drivers/mmc/core/bus.c | 2 ++
16 drivers/mmc/core/sd.c | 34 +++++++++++-----------------------
17 include/linux/mmc/card.h | 1 +
18 3 files changed, 14 insertions(+), 23 deletions(-)
20 --- a/drivers/mmc/core/bus.c
21 +++ b/drivers/mmc/core/bus.c
22 @@ -264,6 +264,8 @@ static void mmc_release_card(struct devi
24 sdio_free_common_cis(card);
26 + kfree(card->ext_reg_buf);
31 --- a/drivers/mmc/core/sd.c
32 +++ b/drivers/mmc/core/sd.c
33 @@ -1026,9 +1026,8 @@ int sd_write_ext_reg(struct mmc_card *ca
34 struct scatterlist sg;
37 - reg_buf = kzalloc(512, GFP_KERNEL);
40 + reg_buf = card->ext_reg_buf;
41 + memset(reg_buf, 0, 512);
45 @@ -1060,8 +1059,6 @@ int sd_write_ext_reg(struct mmc_card *ca
46 mmc_set_data_timeout(&data, card);
47 mmc_wait_for_req(host, &mrq);
52 * Note that, the SD card is allowed to signal busy on DAT0 up to 1s
53 * after the CMD49. Although, let's leave this to be managed by the
54 @@ -1102,9 +1099,7 @@ static int sd_parse_ext_reg_power(struct
58 - reg_buf = kzalloc(512, GFP_KERNEL);
61 + reg_buf = card->ext_reg_buf;
63 /* Read the extension register for power management function. */
64 err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
65 @@ -1134,7 +1129,6 @@ static int sd_parse_ext_reg_power(struct
66 card->ext_power.offset = offset;
73 @@ -1144,9 +1138,7 @@ static int sd_parse_ext_reg_perf(struct
77 - reg_buf = kzalloc(512, GFP_KERNEL);
80 + reg_buf = card->ext_reg_buf;
82 err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
84 @@ -1189,7 +1181,6 @@ static int sd_parse_ext_reg_perf(struct
85 card->ext_perf.offset = offset;
92 @@ -1260,6 +1251,12 @@ static int sd_read_ext_regs(struct mmc_c
96 + card->ext_reg_buf = kzalloc(512, GFP_KERNEL);
97 + if (!card->ext_reg_buf) {
103 * Read 512 bytes of general info, which is found at function number 0,
104 * at page 0 and with no offset.
105 @@ -1326,9 +1323,7 @@ static int sd_flush_cache(struct mmc_hos
106 if (!sd_cache_enabled(host))
109 - reg_buf = kzalloc(512, GFP_KERNEL);
112 + reg_buf = card->ext_reg_buf;
115 * Set Flush Cache at bit 0 in the performance enhancement register at
116 @@ -1364,21 +1359,15 @@ static int sd_flush_cache(struct mmc_hos
117 if (reg_buf[0] & BIT(0))
124 static int sd_enable_cache(struct mmc_card *card)
129 card->ext_perf.feature_enabled &= ~SD_EXT_PERF_CACHE;
131 - reg_buf = kzalloc(512, GFP_KERNEL);
136 * Set Cache Enable at bit 0 in the performance enhancement register at
138 @@ -1397,7 +1386,6 @@ static int sd_enable_cache(struct mmc_ca
139 card->ext_perf.feature_enabled |= SD_EXT_PERF_CACHE;
146 --- a/include/linux/mmc/card.h
147 +++ b/include/linux/mmc/card.h
148 @@ -320,6 +320,7 @@ struct mmc_card {
149 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */
150 struct sd_ext_reg ext_power; /* SD extension reg for PM */
151 struct sd_ext_reg ext_perf; /* SD extension reg for PERF */
152 + u8 *ext_reg_buf; /* 512 byte block for extension register R/W */
154 unsigned int sdio_funcs; /* number of SDIO functions */
155 atomic_t sdio_funcs_probed; /* number of probed SDIO funcs */