Skip to contentSkip to conversation

bus: mhi: host: don't free bhie tables during suspend/hibernation

PATCH Superseded Muhammad Usama Anjum 4 participants 8 messages May 6, 2025 – May 16, 2025

Subsystems (4)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 bus: mhi: host: don't free bhie tables during suspend/hibernation

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Tested-on: WCN6855 WLAN.HSP.1.1-03926.13-QCAHSPSWPL_V2_SILICONZ_CE-2.52297.6

---
Changes since v1:
- Don't free bhie tables during suspend/hibernation only
- Handle fbc_image changed size correctly
- Remove fbc_image getting set to NULL in *free_bhie_table()

Changes since v2:
- Remove the new mhi_partial_unprepare_after_power_down() and instead update mhi_power_down_keep_dev() to use mhi_power_down_unprepare_keep_dev() as suggested by Mani
- Update all users of this API such as ath12k (previously only ath11k was updated)
- Define prev_fw_sz in docs
- Do better alignment of comments

Changes since v3:
- Fix state machine of ath12k by setting ATH12K_MHI_DEINIT with ATH12K_MHI_POWER_OFF_KEEP_DEV state (Thanks Sebastian for testing and finding the problem)
- Use static with mhi_power_down_unprepare_keep_dev()
- Remove crash log as it was showing that kworker wasn't able to allocate memory.

This patch doesn't have fixes tag as we are avoiding error in case of memory pressure. We are just making this driver more robust by not freeing the memory and using the same after resuming.

---
 drivers/bus/mhi/host/boot.c           | 15 +++++++++++----
 drivers/bus/mhi/host/init.c           |  5 +++--
 drivers/bus/mhi/host/pm.c             |  9 +++++++++
 drivers/net/wireless/ath/ath11k/mhi.c |  8 ++++----
 drivers/net/wireless/ath/ath12k/mhi.c | 14 ++++++++++----
 include/linux/mhi.h                   |  2 ++
 6 files changed, 39 insertions(+), 14 deletions(-)
drivers/bus/mhi/host/boot.c +11 -4
index efa3b6dddf4d2..bc8459798bbee 100644
@@ -584,10 +584,17 @@ void mhi_fw_load_handler(struct mhi_controller *mhi_cntrl)
 	 * device transitioning into MHI READY state
 	 */
 	if (fw_load_type == MHI_FW_LOAD_FBC) {
-		ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image, fw_sz);
-		if (ret) {
-			release_firmware(firmware);
-			goto error_fw_load;
+		if (mhi_cntrl->fbc_image && fw_sz != mhi_cntrl->prev_fw_sz) {
+			mhi_free_bhie_table(mhi_cntrl, mhi_cntrl->fbc_image);
+			mhi_cntrl->fbc_image = NULL;
+		}
+		if (!mhi_cntrl->fbc_image) {
+			ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image, fw_sz);
+			if (ret) {
+				release_firmware(firmware);
+				goto error_fw_load;
+			}
+			mhi_cntrl->prev_fw_sz = fw_sz;
 		}
 
 		/* Load the firmware into BHIE vec table */
drivers/bus/mhi/host/init.c +3 -2
index 13e7a55f54ff4..a7663ad16bfc6 100644
@@ -1173,8 +1173,9 @@ int mhi_prepare_for_power_up(struct mhi_controller *mhi_cntrl)
 		/*
 		 * Allocate RDDM table for debugging purpose if specified
 		 */
-		mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->rddm_image,
-				     mhi_cntrl->rddm_size);
+		if (!mhi_cntrl->rddm_image)
+			mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->rddm_image,
+					     mhi_cntrl->rddm_size);
 		if (mhi_cntrl->rddm_image) {
 			ret = mhi_rddm_prepare(mhi_cntrl,
 					       mhi_cntrl->rddm_image);
drivers/bus/mhi/host/pm.c +9 -0
index e6c3ff62bab1d..107d71b4cc51a 100644
@@ -1259,10 +1259,19 @@ void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful)
 }
 EXPORT_SYMBOL_GPL(mhi_power_down);
 
+static void __mhi_power_down_unprepare_keep_dev(struct mhi_controller *mhi_cntrl)
+{
+	mhi_cntrl->bhi = NULL;
+	mhi_cntrl->bhie = NULL;
+
+	mhi_deinit_dev_ctxt(mhi_cntrl);
+}
+
 void mhi_power_down_keep_dev(struct mhi_controller *mhi_cntrl,
 			       bool graceful)
 {
 	__mhi_power_down(mhi_cntrl, graceful, false);
+	__mhi_power_down_unprepare_keep_dev(mhi_cntrl);
 }
 EXPORT_SYMBOL_GPL(mhi_power_down_keep_dev);
 
drivers/net/wireless/ath/ath11k/mhi.c +4 -4
index acd76e9392d31..c5dc776b23643 100644
@@ -460,12 +460,12 @@ void ath11k_mhi_stop(struct ath11k_pci *ab_pci, bool is_suspend)
 	 * workaround, otherwise ath11k_core_resume() will timeout
 	 * during resume.
 	 */
-	if (is_suspend)
+	if (is_suspend) {
 		mhi_power_down_keep_dev(ab_pci->mhi_ctrl, true);
-	else
+	} else {
 		mhi_power_down(ab_pci->mhi_ctrl, true);
-
-	mhi_unprepare_after_power_down(ab_pci->mhi_ctrl);
+		mhi_unprepare_after_power_down(ab_pci->mhi_ctrl);
+	}
 }
 
 int ath11k_mhi_suspend(struct ath11k_pci *ab_pci)
drivers/net/wireless/ath/ath12k/mhi.c +10 -4
index 08f44baf182a5..3af524ccf4a5a 100644
@@ -601,6 +601,12 @@ static int ath12k_mhi_set_state(struct ath12k_pci *ab_pci,
 
 	ath12k_mhi_set_state_bit(ab_pci, mhi_state);
 
+	/* mhi_power_down_keep_dev() has been updated to DEINIT without
+	 * freeing bhie tables
+	 */
+	if (mhi_state == ATH12K_MHI_POWER_OFF_KEEP_DEV)
+		ath12k_mhi_set_state_bit(ab_pci, ATH12K_MHI_DEINIT);
+
 	return 0;
 
 out:
@@ -635,12 +641,12 @@ void ath12k_mhi_stop(struct ath12k_pci *ab_pci, bool is_suspend)
 	 * workaround, otherwise ath12k_core_resume() will timeout
 	 * during resume.
 	 */
-	if (is_suspend)
+	if (is_suspend) {
 		ath12k_mhi_set_state(ab_pci, ATH12K_MHI_POWER_OFF_KEEP_DEV);
-	else
+	} else {
 		ath12k_mhi_set_state(ab_pci, ATH12K_MHI_POWER_OFF);
-
-	ath12k_mhi_set_state(ab_pci, ATH12K_MHI_DEINIT);
+		ath12k_mhi_set_state(ab_pci, ATH12K_MHI_DEINIT);
+	}
 }
 
 void ath12k_mhi_suspend(struct ath12k_pci *ab_pci)
include/linux/mhi.h +2 -0
index dd372b0123a6d..6fd218a877855 100644
@@ -306,6 +306,7 @@ struct mhi_controller_config {
  *           if fw_image is NULL and fbc_download is true (optional)
  * @fw_sz: Firmware image data size for normal booting, used only if fw_image
  *         is NULL and fbc_download is true (optional)
+ * @prev_fw_sz: Previous firmware image data size, when fbc_download is true
  * @edl_image: Firmware image name for emergency download mode (optional)
  * @rddm_size: RAM dump size that host should allocate for debugging purpose
  * @sbl_size: SBL image size downloaded through BHIe (optional)
@@ -382,6 +383,7 @@ struct mhi_controller {
 	const char *fw_image;
 	const u8 *fw_data;
 	size_t fw_sz;
+	size_t prev_fw_sz;
 	const char *edl_image;
 	size_t rddm_size;
 	size_t sbl_size;
Signature · 1 line
2.43.0
Tested-by on v4 1/1
#1 lore raw
Quoted 10 lines · Muhammad Usama Anjum

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

Quoted 7 lines

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Tested-on: WCN6855 WLAN.HSP.1.1-03926.13-QCAHSPSWPL_V2_SILICONZ_CE-2.52297.6

with WLAN firmware unchanged, suspend/resume is working well:

Tested-on: WCN7850 hw2.0 WLAN.HMT.1.1.c5-00284-QCAHMTSWPL_V1.0_V2.0_SILICONZ-3

Acked-by on v4 1/1
#2 lore raw
In reply to Muhammad Usama Anjum
Quoted 10 lines · Muhammad Usama Anjum

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

Quoted 7 lines

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Tested-on: WCN6855 WLAN.HSP.1.1-03926.13-QCAHSPSWPL_V2_SILICONZ_CE-2.52297.6

Mani, if this looks ok to you I assume you'll take this through your MHI tree.

reviewer
#3 lore raw
In reply to Muhammad Usama Anjum
Quoted 5 lines · Muhammad Usama Anjum

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

Quoted 8 lines

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Why is it valid to load new firmware as a result of suspend? I don't users would expect that.

drivers/bus/mhi/host/boot.c void mhi_fw_load_handler(struct mhi_controller *mhi_cntrl) · line 586

Quoted 8 lines
drivers/bus/mhi/host/boot.c +0 -0
index efa3b6dddf4d2..bc8459798bbee 100644
@@ -584,10 +584,17 @@ void mhi_fw_load_handler(struct mhi_controller *mhi_cntrl)
  	 * device transitioning into MHI READY state
  	 */
  	if (fw_load_type == MHI_FW_LOAD_FBC) {

Why is this FBC specific?

Quoted 23 lines
drivers/bus/mhi/host/boot.c +11 -4
-		ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image, fw_sz);
-		if (ret) {
-			release_firmware(firmware);
-			goto error_fw_load;
+		if (mhi_cntrl->fbc_image && fw_sz != mhi_cntrl->prev_fw_sz) {
+			mhi_free_bhie_table(mhi_cntrl, mhi_cntrl->fbc_image);
+			mhi_cntrl->fbc_image = NULL;
+		}
+		if (!mhi_cntrl->fbc_image) {
+			ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image, fw_sz);
+			if (ret) {
+				release_firmware(firmware);
+				goto error_fw_load;
+			}
+			mhi_cntrl->prev_fw_sz = fw_sz;
  		}
 
  		/* Load the firmware into BHIE vec table */
drivers/bus/mhi/host/pm.c +0 -0
index e6c3ff62bab1d..107d71b4cc51a 100644

drivers/bus/mhi/host/pm.c void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful) · line 1265

Quoted 7 lines
drivers/bus/mhi/host/pm.c +4 -0
@@ -1259,10 +1259,19 @@ void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful)
  }
  EXPORT_SYMBOL_GPL(mhi_power_down);
 
+static void __mhi_power_down_unprepare_keep_dev(struct mhi_controller *mhi_cntrl)
+{
+	mhi_cntrl->bhi = NULL;
+	mhi_cntrl->bhie = NULL;

Why?

Quoted 3 lines
drivers/bus/mhi/host/pm.c +3 -0
+
+	mhi_deinit_dev_ctxt(mhi_cntrl);
+}
#4 lore raw
Quoted 11 lines · Jeff Hugo
Quoted 10 lines · Muhammad Usama Anjum

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

Quoted 6 lines
Quoted 3 lines

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Why is it valid to load new firmware as a result of suspend?  I don't users would expect that.

I'm not sure its valid or not. Like other users, I also don't expect that firmware would get changed. It doesn't seem to be tested and hence supported case.

But other drivers have code which have implementation like this. I'd mentioned previously that this patch was motivated from the ath12k [1] and ath11k [2] patches. They don't free the memory and reuse the same memory if new size is same.

Quoted 12 lines

drivers/bus/mhi/host/boot.c void mhi_fw_load_handler(struct mhi_controller · line 587

Quoted 10 lines
drivers/bus/mhi/host/boot.c +0 -0
index efa3b6dddf4d2..bc8459798bbee 100644
@@ -584,10 +584,17 @@ void mhi_fw_load_handler(struct mhi_controller
 *mhi_cntrl)
        * device transitioning into MHI READY state
        */
       if (fw_load_type == MHI_FW_LOAD_FBC) {

Why is this FBC specific?

It seems we allocate fbc_image only when firmware load type is FW_LOAD_FBC. I'm just optimizing the buffer allocation here.

Quoted 27 lines
Quoted 27 lines
drivers/bus/mhi/host/boot.c +11 -4
-        ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image,
 fw_sz);
-        if (ret) {
-            release_firmware(firmware);
-            goto error_fw_load;
+        if (mhi_cntrl->fbc_image && fw_sz != mhi_cntrl->prev_fw_sz) {
+            mhi_free_bhie_table(mhi_cntrl, mhi_cntrl->fbc_image);
+            mhi_cntrl->fbc_image = NULL;
+        }
+        if (!mhi_cntrl->fbc_image) {
+            ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl-
 >fbc_image, fw_sz);
+            if (ret) {
+                release_firmware(firmware);
+                goto error_fw_load;
+            }
+            mhi_cntrl->prev_fw_sz = fw_sz;

          }
            /* Load the firmware into BHIE vec table */

drivers/bus/mhi/host/pm.c +0 -0
index e6c3ff62bab1d..107d71b4cc51a 100644
@@ -1259,10 +1259,19 @@ void mhi_power_down(struct mhi_controller
 *mhi_cntrl, bool graceful)
   }
Quoted 8 lines
Quoted 6 lines
drivers/bus/mhi/host/pm.c +0 -0
   EXPORT_SYMBOL_GPL(mhi_power_down);

  +static void __mhi_power_down_unprepare_keep_dev(struct mhi_controller *mhi_cntrl)
+{
+    mhi_cntrl->bhi = NULL;
+    mhi_cntrl->bhie = NULL;

Why?

This function is shorter version of mhi_unprepare_after_power_down(). As we need different code path in case of suspend/hibernation case, I was adding a new API which Mani asked me remove and consolidate into mhi_power_down_keep_dev() instead. So this static function has been added. [3]

Quoted 4 lines

drivers/bus/mhi/host/pm.c void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful) · line 1268

Quoted 4 lines
drivers/bus/mhi/host/pm.c +3 -0
+
+    mhi_deinit_dev_ctxt(mhi_cntrl);
+}

[1]
https://lore.kernel.org/all/[email protected]/
[2]
https://lore.kernel.org/all/[email protected]/
[3]
https://lore.kernel.org/all/y5odcxzms6mwpz5bdxhbjxo7p6whsdgwm772usmmzqobhf6nam@p4ul7vn7d3an

Signature · 2 lines
Regards,
Usama
reviewer
#5 lore raw
Quoted 18 lines · Muhammad Usama Anjum
Quoted 11 lines · Jeff Hugo
Quoted 10 lines · Muhammad Usama Anjum

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

Quoted 6 lines
Quoted 3 lines

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Why is it valid to load new firmware as a result of suspend?  I don't users would expect that.

Quoted 8 lines

I'm not sure its valid or not. Like other users, I also don't expect that firmware would get changed. It doesn't seem to be tested and hence supported case.

But other drivers have code which have implementation like this. I'd mentioned previously that this patch was motivated from the ath12k [1] and ath11k [2] patches. They don't free the memory and reuse the same memory if new size is same.

It feels like this justification needs to be detailed in the commit text. I suspect at some point we'll have another MHI device where the FW will need to be cached.

Quoted 11 lines
Quoted 11 lines

drivers/bus/mhi/host/boot.c void mhi_fw_load_handler(struct mhi_controller · line 587

Quoted 9 lines
drivers/bus/mhi/host/boot.c +0 -0
index efa3b6dddf4d2..bc8459798bbee 100644
@@ -584,10 +584,17 @@ void mhi_fw_load_handler(struct mhi_controller
 *mhi_cntrl)
        * device transitioning into MHI READY state
        */
       if (fw_load_type == MHI_FW_LOAD_FBC) {

Why is this FBC specific?

Quoted 2 lines

It seems we allocate fbc_image only when firmware load type is FW_LOAD_FBC. I'm just optimizing the buffer allocation here.

We alloc bhie tables in non FBC usecases. Is this somehow an FBC specific issue? Perhaps you could clarify the limits of this solution in the commit text?

Quoted 36 lines
Quoted 28 lines
Quoted 28 lines
drivers/bus/mhi/host/boot.c +11 -4
-        ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image,
 fw_sz);
-        if (ret) {
-            release_firmware(firmware);
-            goto error_fw_load;
+        if (mhi_cntrl->fbc_image && fw_sz != mhi_cntrl->prev_fw_sz) {
+            mhi_free_bhie_table(mhi_cntrl, mhi_cntrl->fbc_image);
+            mhi_cntrl->fbc_image = NULL;
+        }
+        if (!mhi_cntrl->fbc_image) {
+            ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl-
 > fbc_image, fw_sz);
+            if (ret) {
+                release_firmware(firmware);
+                goto error_fw_load;
+            }
+            mhi_cntrl->prev_fw_sz = fw_sz;

          }
            /* Load the firmware into BHIE vec table */

drivers/bus/mhi/host/pm.c +0 -0
index e6c3ff62bab1d..107d71b4cc51a 100644
@@ -1259,10 +1259,19 @@ void mhi_power_down(struct mhi_controller
 *mhi_cntrl, bool graceful)
   }
Quoted 8 lines

drivers/bus/mhi/host/pm.c void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful) · line 1265

Quoted 6 lines
drivers/bus/mhi/host/pm.c +3 -0
   EXPORT_SYMBOL_GPL(mhi_power_down);
   +static void __mhi_power_down_unprepare_keep_dev(struct
 mhi_controller *mhi_cntrl)
+{
+    mhi_cntrl->bhi = NULL;
+    mhi_cntrl->bhie = NULL;

Why?

Quoted 5 lines

This function is shorter version of mhi_unprepare_after_power_down(). As we need different code path in case of suspend/hibernation case, I was adding a new API which Mani asked me remove and consolidate into mhi_power_down_keep_dev() instead. So this static function has been added. [3]

I don't understand the need to zero these out. Also, if you are copying part of the functionality of mhi_unprepare_after_power_down(), shouldn't that functionality be moved into your new API to eliminate duplication?

#6 lore raw
Quoted 23 lines · Jeff Hugo
Quoted 22 lines · Muhammad Usama Anjum
Quoted 11 lines · Jeff Hugo
Quoted 10 lines · Muhammad Usama Anjum

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

Quoted 6 lines
Quoted 3 lines

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Why is it valid to load new firmware as a result of suspend?  I don't users would expect that.

I'm not sure its valid or not. Like other users, I also don't expect that firmware would get changed. It doesn't seem to be tested and hence supported case.

Quoted 8 lines
Quoted 4 lines

But other drivers have code which have implementation like this. I'd mentioned previously that this patch was motivated from the ath12k [1] and ath11k [2] patches. They don't free the memory and reuse the same memory if new size is same.

It feels like this justification needs to be detailed in the commit text. I suspect at some point we'll have another MHI device where the FW will need to be cached.

Okay. I'll add this information to the commit message. Currently I've not seen firmware caching being used other than graphics driver.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

drivers/bus/mhi/host/boot.c void mhi_fw_load_handler(struct mhi_controller · line 587

Quoted 10 lines
drivers/bus/mhi/host/boot.c +0 -0
index efa3b6dddf4d2..bc8459798bbee 100644
@@ -584,10 +584,17 @@ void mhi_fw_load_handler(struct mhi_controller
 *mhi_cntrl)
         * device transitioning into MHI READY state
         */
        if (fw_load_type == MHI_FW_LOAD_FBC) {

Why is this FBC specific?

Quoted 6 lines
Quoted 2 lines

It seems we allocate fbc_image only when firmware load type is FW_LOAD_FBC. I'm just optimizing the buffer allocation here.

We alloc bhie tables in non FBC usecases. Is this somehow an FBC specific issue? Perhaps you could clarify the limits of this solution in the commit text?

Okay. I'll add information that we are optimizing the bhie allocations. It has nothing to do with firmware type. I've found only 2 bhie allocations; fbc_image and rddm_image. So we are optimizing those.

Quoted 43 lines
Quoted 37 lines
Quoted 29 lines
Quoted 29 lines
drivers/bus/mhi/host/boot.c +11 -4
-        ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image,
 fw_sz);
-        if (ret) {
-            release_firmware(firmware);
-            goto error_fw_load;
+        if (mhi_cntrl->fbc_image && fw_sz != mhi_cntrl->prev_fw_sz) {
+            mhi_free_bhie_table(mhi_cntrl, mhi_cntrl->fbc_image);
+            mhi_cntrl->fbc_image = NULL;
+        }
+        if (!mhi_cntrl->fbc_image) {
+            ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl-
 > fbc_image, fw_sz);
+            if (ret) {
+                release_firmware(firmware);
+                goto error_fw_load;
+            }
+            mhi_cntrl->prev_fw_sz = fw_sz;

           }
             /* Load the firmware into BHIE vec table */

drivers/bus/mhi/host/pm.c +0 -0
index e6c3ff62bab1d..107d71b4cc51a 100644
@@ -1259,10 +1259,19 @@ void mhi_power_down(struct mhi_controller
 *mhi_cntrl, bool graceful)
    }
Quoted 8 lines
Quoted 6 lines
drivers/bus/mhi/host/pm.c +0 -0
    EXPORT_SYMBOL_GPL(mhi_power_down);

   +static void __mhi_power_down_unprepare_keep_dev(struct mhi_controller *mhi_cntrl)
+{
+    mhi_cntrl->bhi = NULL;
+    mhi_cntrl->bhie = NULL;

Why?

Quoted 5 lines

This function is shorter version of mhi_unprepare_after_power_down(). As we need different code path in case of suspend/hibernation case, I was adding a new API which Mani asked me remove and consolidate into mhi_power_down_keep_dev() instead. So this static function has been added. [3]

Quoted 3 lines

I don't understand the need to zero these out.  Also, if you are copying part of the functionality of mhi_unprepare_after_power_down(), shouldn't that functionality be moved into your new API to eliminate duplication?

This how the cleanup works mhi_unprepare_after_power_down(). Yeah, it makes sense to use this function in mhi_unprepare_after_power_down().

Sending next version soon.

Quoted 1 line
Signature · 2 lines
Regards,
Usama
reviewer
#7 lore raw
Quoted 50 lines · Muhammad Usama Anjum
Quoted 23 lines · Jeff Hugo
Quoted 22 lines · Muhammad Usama Anjum
Quoted 11 lines · Jeff Hugo
Quoted 10 lines · Muhammad Usama Anjum

Fix dma_direct_alloc() failure at resume time during bhie_table allocation because of memory pressure. There is a report where at resume time, the memory from the dma doesn't get allocated and MHI fails to re-initialize.

To fix it, don't free the memory at power down during suspend / hibernation. Instead, use the same allocated memory again after every resume / hibernation. This patch has been tested with resume and hibernation both.

Quoted 6 lines
Quoted 3 lines

The rddm is of constant size for a given hardware. While the fbc_image size depends on the firmware. If the firmware changes, we'll free and allocate new memory for it.

Why is it valid to load new firmware as a result of suspend?  I don't users would expect that.

I'm not sure its valid or not. Like other users, I also don't expect that firmware would get changed. It doesn't seem to be tested and hence supported case.

Quoted 8 lines
Quoted 4 lines

But other drivers have code which have implementation like this. I'd mentioned previously that this patch was motivated from the ath12k [1] and ath11k [2] patches. They don't free the memory and reuse the same memory if new size is same.

It feels like this justification needs to be detailed in the commit text. I suspect at some point we'll have another MHI device where the FW will need to be cached.

Okay. I'll add this information to the commit message. Currently I've not seen firmware caching being used other than graphics driver.

Quoted 15 lines
Quoted 14 lines
Quoted 12 lines

drivers/bus/mhi/host/boot.c void mhi_fw_load_handler(struct mhi_controller · line 587

Quoted 10 lines
drivers/bus/mhi/host/boot.c +0 -0
index efa3b6dddf4d2..bc8459798bbee 100644
@@ -584,10 +584,17 @@ void mhi_fw_load_handler(struct mhi_controller
 *mhi_cntrl)
         * device transitioning into MHI READY state
         */
        if (fw_load_type == MHI_FW_LOAD_FBC) {

Why is this FBC specific?

It seems we allocate fbc_image only when firmware load type is FW_LOAD_FBC. I'm just optimizing the buffer allocation here.

Quoted 6 lines
Quoted 3 lines

We alloc bhie tables in non FBC usecases. Is this somehow an FBC specific issue? Perhaps you could clarify the limits of this solution in the commit text?

Okay. I'll add information that we are optimizing the bhie allocations. It has nothing to do with firmware type. I've found only 2 bhie allocations; fbc_image and rddm_image. So we are optimizing those.

There is a 3rd allocation, and it has everything to do with firmware type. Did you miss mhi_load_image_bhie()? I'm not asking you to touch mhi_load_image_bhie(), but to recognize that what you are doing is specific to some BHIe devices, not all.

Quoted 54 lines
Quoted 44 lines
Quoted 38 lines
Quoted 30 lines
Quoted 30 lines
drivers/bus/mhi/host/boot.c +11 -4
-        ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image,
 fw_sz);
-        if (ret) {
-            release_firmware(firmware);
-            goto error_fw_load;
+        if (mhi_cntrl->fbc_image && fw_sz != mhi_cntrl->prev_fw_sz) {
+            mhi_free_bhie_table(mhi_cntrl, mhi_cntrl->fbc_image);
+            mhi_cntrl->fbc_image = NULL;
+        }
+        if (!mhi_cntrl->fbc_image) {
+            ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl-
 > fbc_image, fw_sz);
+            if (ret) {
+                release_firmware(firmware);
+                goto error_fw_load;
+            }
+            mhi_cntrl->prev_fw_sz = fw_sz;

           }
             /* Load the firmware into BHIE vec table */

drivers/bus/mhi/host/pm.c +0 -0
index e6c3ff62bab1d..107d71b4cc51a 100644
@@ -1259,10 +1259,19 @@ void mhi_power_down(struct mhi_controller
 *mhi_cntrl, bool graceful)
    }
Quoted 8 lines

drivers/bus/mhi/host/pm.c void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful) · line 1265

Quoted 6 lines
drivers/bus/mhi/host/pm.c +3 -0
    EXPORT_SYMBOL_GPL(mhi_power_down);
    +static void __mhi_power_down_unprepare_keep_dev(struct
 mhi_controller *mhi_cntrl)
+{
+    mhi_cntrl->bhi = NULL;
+    mhi_cntrl->bhie = NULL;

Why?

Quoted 5 lines

This function is shorter version of mhi_unprepare_after_power_down(). As we need different code path in case of suspend/hibernation case, I was adding a new API which Mani asked me remove and consolidate into mhi_power_down_keep_dev() instead. So this static function has been added. [3]

Quoted 3 lines

I don't understand the need to zero these out.  Also, if you are copying part of the functionality of mhi_unprepare_after_power_down(), shouldn't that functionality be moved into your new API to eliminate duplication?

This how the cleanup works mhi_unprepare_after_power_down(). Yeah, it makes sense to use this function in mhi_unprepare_after_power_down().

Sending next version soon.

Quoted 1 line