MHI patches for v5.8
Subsystems (2)
[email protected]
maintainercover MHI patches for v5.8
From: Manivannan Sadhasivam <[email protected]>
Hi Greg,
Here is the set of MHI patches for v5.8. Most of the patches are cleanup and refactoring ones. All of them are reviewed by myself and Jeff and also verified on x86 and ARM64 architectures for functionality.
Here is the short summary:
-------------------------------------------------------------
- The firmware download was handled by a worker thread which gets scheduled when the device powers up. But this thread waits until the device gets into PBL state (notified using PM state worker). Sometimes, there might be delay for the device to enter PBL state and due to that the firmware worker thread will timeout. So in order to handle this situation effectively, the firmware load is now directly called by PM state worker instead of scheduling the thread.
- Return proper error codes incase of error while loading the AMSS firmware through BHIE protocol
- The MHI register space of the device accepts only non-zero values for the sequence identifier. But there is a possibility that the host might write zero (due to the use of prandom_u32() API). Hence, a macro is introduced which provides non-zero sequence identifiers and used them in all places.
- Moved all common TRE generation code to mhi_gen_tre() function
- The MHI host reads channel ID from the event ring element of the client device. This ID can be of any value between 0 to 255 but the host may not support all those IDs. So reject the event ring elements whose channel IDs are not within the limits of the controller.
- Limit the transfer length read from the client device. This value should be within the size of the MHI host buffer but there are chances this can be larger.
- Remove the system worker thread for processing the SYS_ERR condition and instead call the function directly from EE worker. This is done to avoid any possible race while MHI shutting down.
- Handle MHI power off in the state worker thread as like MISSION_MODE. This helps in preventing a possible race condition where a power off is issued by the controller while processing mission mode.
- Skip the handling of BHI interrupt when the register access is not allowed due to the device in wrong PM state.
- The write_lock of 'mhi_chan->lock' should only protect 'db_mode'. Hence, use it properly in places where it is protecting other unwanted regions.
- Reset the client device if it is in SYS_ERR state during power up.
-------------------------------------------------------------
Please consider merging!
Thanks,
Mani
Bhaumik Bhatt (4):
bus: mhi: core: Handle firmware load using state worker
bus: mhi: core: Return appropriate error codes for AMSS load failure
bus: mhi: core: Improve debug logs for loading firmware
bus: mhi: core: Ensure non-zero session or sequence ID values are used
Hemant Kumar (9):
bus: mhi: core: Refactor mhi queue APIs
bus: mhi: core: Cache intmod from mhi event to mhi channel
bus: mhi: core: Add range check for channel id received in event ring
bus: mhi: core: Read transfer length from an event properly
bus: mhi: core: Remove the system error worker thread
bus: mhi: core: Handle disable transitions in state worker
bus: mhi: core: Skip handling BHI irq if MHI reg access is not allowed
bus: mhi: core: Do not process SYS_ERROR if RDDM is supported
bus: mhi: core: Handle write lock properly in mhi_pm_m0_transition
Jeffrey Hugo (1):
bus: mhi: core: Handle syserr during power_up
drivers/bus/mhi/core/boot.c | 75 ++++++------ drivers/bus/mhi/core/init.c | 8 +- drivers/bus/mhi/core/internal.h | 9 +- drivers/bus/mhi/core/main.c | 194 ++++++++++++++++++-------------- drivers/bus/mhi/core/pm.c | 86 +++++++++----- include/linux/mhi.h | 4 - 6 files changed, 217 insertions(+), 159 deletions(-)
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 1/14 bus: mhi: core: Refactor mhi queue APIs
From: Hemant Kumar <[email protected]>
Move all the common code to generate TRE from mhi_queue_buf, mhi_queue_dma and mhi_queue_skb to mhi_gen_tre. This helps to centralize the TRE generation code which makes any future bug fixing easier to manage in these APIs.
--- drivers/bus/mhi/core/internal.h | 3 +- drivers/bus/mhi/core/main.c | 107 ++++++++++++++------------------ 2 files changed, 47 insertions(+), 63 deletions(-)
drivers/bus/mhi/core/internal.h +1 -2
@@ -670,8 +670,7 @@ irqreturn_t mhi_intvec_threaded_handler(int irq_number, void *dev);irqreturn_t mhi_intvec_handler (int irq_number,void *dev);int mhi_gen_tre (struct mhi_controller *mhi_cntrl,struct mhi_chan *mhi_chan, -void *buf,void *cb,size_t buf_len,enum mhi_flags flags); - +struct mhi_buf_info *info,enum mhi_flags flags);int mhi_map_single_no_bb (struct mhi_controller *mhi_cntrl,struct mhi_buf_info *buf_info); int mhi_map_single_use_bb(struct mhi_controller *mhi_cntrl,
drivers/bus/mhi/core/main.c +46 -61
184 lines not on this page · raw message
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 2/14 bus: mhi: core: Cache intmod from mhi event to mhi channel
In reply to [email protected]From: Hemant Kumar <[email protected]>
Driver is using zero initialized intmod value from mhi channel when configuring TRE for bei field. This prevents interrupt moderation to take effect in case it is supported by an event ring. Fix this by copying intmod value from associated event ring to mhi channel upon registering mhi controller.
--- drivers/bus/mhi/core/init.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/bus/mhi/core/init.c +4 -0
@@ -863,6 +863,10 @@ int mhi_register_controller(struct mhi_controller *mhi_cntrl,mutex_init (&mhi_chan->mutex);init_completion (&mhi_chan->completion);rwlock_init (&mhi_chan->lock); + +/* used in setting bei field of TRE */ + mhi_event = &mhi_cntrl->mhi_event[mhi_chan->er_index]; + mhi_chan->intmod = mhi_event->intmod; }if (mhi_cntrl->bounce_buf) {
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 3/14 bus: mhi: core: Add range check for channel id received in event ring
In reply to [email protected]From: Hemant Kumar <[email protected]>
MHI data completion handler function reads channel id from event ring element. Value is under the control of MHI devices and can be any value between 0 and 255. In order to prevent out of bound access add a bound check against the max channel supported by controller and skip processing of that event ring element.
--- drivers/bus/mhi/core/main.c | 40 +++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 11 deletions(-)
drivers/bus/mhi/core/main.c +29 -11
@@ -774,9 +774,18 @@ int mhi_process_ctrl_ev_ring(struct mhi_controller *mhi_cntrl, }case MHI_PKT_TYPE_TX_EVENT : chan =MHI_TRE_GET_EV_CHID (local_rp); - mhi_chan = &mhi_cntrl->mhi_chan[chan]; -parse_xfer_event (mhi_cntrl, local_rp, mhi_chan); - event_quota--; + +WARN_ON (chan >= mhi_cntrl->max_chan); + +/* +* Only process the event ring elements whose channel +* ID is within the maximum supported range. +*/ +if (chan < mhi_cntrl->max_chan) { + mhi_chan = &mhi_cntrl->mhi_chan[chan]; +parse_xfer_event (mhi_cntrl, local_rp, mhi_chan); + event_quota--; + }break ;default :dev_err (dev,"Unhandled event type: %d\n" , type);
@@ -819,14 +828,23 @@ int mhi_process_data_event_ring(struct mhi_controller *mhi_cntrl,enum mhi_pkt_type type =MHI_TRE_GET_EV_TYPE (local_rp); chan =MHI_TRE_GET_EV_CHID (local_rp); - mhi_chan = &mhi_cntrl->mhi_chan[chan]; - -if (likely (type ==MHI_PKT_TYPE_TX_EVENT )) { -parse_xfer_event (mhi_cntrl, local_rp, mhi_chan); - event_quota--; - }else if (type ==MHI_PKT_TYPE_RSC_TX_EVENT ) { -parse_rsc_event (mhi_cntrl, local_rp, mhi_chan); - event_quota--; + +WARN_ON (chan >= mhi_cntrl->max_chan); + +/* +* Only process the event ring elements whose channel +* ID is within the maximum supported range. +*/ +if (chan < mhi_cntrl->max_chan) { + mhi_chan = &mhi_cntrl->mhi_chan[chan]; + +if (likely (type ==MHI_PKT_TYPE_TX_EVENT )) { +parse_xfer_event (mhi_cntrl, local_rp, mhi_chan); + event_quota--; + }else if (type ==MHI_PKT_TYPE_RSC_TX_EVENT ) { +parse_rsc_event (mhi_cntrl, local_rp, mhi_chan); + event_quota--; + } }mhi_recycle_ev_ring_element (mhi_cntrl, ev_ring);
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 4/14 bus: mhi: core: Read transfer length from an event properly
In reply to [email protected]From: Hemant Kumar <[email protected]>
When MHI Driver receives an EOT event, it reads xfer_len from the event in the last TRE. The value is under control of the MHI device and never validated by Host MHI driver. The value should never be larger than the real size of the buffer but a malicious device can set the value 0xFFFF as maximum. This causes driver to memory overflow (both read or write). Fix this issue by reading minimum of transfer length from event and the buffer length provided.
--- drivers/bus/mhi/core/main.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/bus/mhi/core/main.c +7 -2
@@ -513,7 +513,10 @@ static int parse_xfer_event(struct mhi_controller *mhi_cntrl, mhi_cntrl->unmap_single (mhi_cntrl, buf_info); result.buf_addr = buf_info->cb_buf; - result.bytes_xferd = xfer_len; + +/* truncate to buf len if xfer_len is larger */ + result.bytes_xferd = +min_t (u16, xfer_len, buf_info->len);mhi_del_ring_element (mhi_cntrl, buf_ring);mhi_del_ring_element (mhi_cntrl, tre_ring); local_rp = tre_ring->rp;
@@ -597,7 +600,9 @@ static int parse_rsc_event(struct mhi_controller *mhi_cntrl, result.transaction_status = (ev_code ==MHI_EV_CC_OVERFLOW ) ? -EOVERFLOW :0 ; - result.bytes_xferd = xfer_len; + +/* truncate to buf len if xfer_len is larger */ + result.bytes_xferd =min_t (u16, xfer_len, buf_info->len); result.buf_addr = buf_info->cb_buf; result.dir = mhi_chan->dir;
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 5/14 bus: mhi: core: Handle firmware load using state worker
In reply to [email protected]From: Bhaumik Bhatt <[email protected]>
Upon power up, driver queues firmware worker thread if the execution environment is PBL. Firmware worker is blocked with a timeout until state worker gets a chance to run and unblock firmware worker. An endpoint power up failure can be seen if state worker gets a chance to run after firmware worker has timed out. Remove this dependency and handle firmware load directly using state worker thread.
--- drivers/bus/mhi/core/boot.c | 18 +++--------------- drivers/bus/mhi/core/init.c | 1 - drivers/bus/mhi/core/internal.h | 1 + drivers/bus/mhi/core/pm.c | 6 +----- include/linux/mhi.h | 2 -- 5 files changed, 5 insertions(+), 23 deletions(-)
drivers/bus/mhi/core/boot.c +3 -15
@@ -377,30 +377,18 @@ static void mhi_firmware_copy(struct mhi_controller *mhi_cntrl, } } -void mhi_fw_load_worker (struct work_struct *work) +void mhi_fw_load_handler (struct mhi_controller *mhi_cntrl) { -struct mhi_controller *mhi_cntrl;const struct firmware *firmware =NULL ;struct image_info *image_info; -struct device *dev; +struct device *dev = &mhi_cntrl->mhi_dev->dev;const char *fw_name;void *buf;dma_addr_t dma_addr;size_t size;int ret; - mhi_cntrl = container_of(work, struct mhi_controller, fw_worker); - dev = &mhi_cntrl->mhi_dev->dev; - -dev_dbg (dev,"Waiting for device to enter PBL from: %s\n" , -TO_MHI_EXEC_STR (mhi_cntrl->ee)); - - ret =wait_event_timeout (mhi_cntrl->state_event, -MHI_IN_PBL (mhi_cntrl->ee) || -MHI_PM_IN_ERROR_STATE (mhi_cntrl->pm_state), -msecs_to_jiffies (mhi_cntrl->timeout_ms)); - -if (!ret ||MHI_PM_IN_ERROR_STATE (mhi_cntrl->pm_state)) { +if (MHI_PM_IN_ERROR_STATE (mhi_cntrl->pm_state)) {dev_err (dev,"Device MHI is not in valid state\n" );return ; }
drivers/bus/mhi/core/init.c +0 -1
@@ -835,7 +835,6 @@ int mhi_register_controller(struct mhi_controller *mhi_cntrl,spin_lock_init (&mhi_cntrl->wlock);INIT_WORK (&mhi_cntrl->st_worker, mhi_pm_st_worker);INIT_WORK (&mhi_cntrl->syserr_worker, mhi_pm_sys_err_worker); -INIT_WORK (&mhi_cntrl->fw_worker, mhi_fw_load_worker);init_waitqueue_head (&mhi_cntrl->state_event); mhi_cmd = mhi_cntrl->mhi_cmd;
drivers/bus/mhi/core/internal.h +1 -0
@@ -627,6 +627,7 @@ int mhi_init_irq_setup(struct mhi_controller *mhi_cntrl);void mhi_deinit_free_irq (struct mhi_controller *mhi_cntrl);void mhi_rddm_prepare (struct mhi_controller *mhi_cntrl,struct image_info *img_info); +void mhi_fw_load_handler (struct mhi_controller *mhi_cntrl);int mhi_prepare_channel (struct mhi_controller *mhi_cntrl,struct mhi_chan *mhi_chan); int mhi_init_chan_ctxt(struct mhi_controller *mhi_cntrl,
drivers/bus/mhi/core/pm.c +1 -5
@@ -528,7 +528,6 @@ static void mhi_pm_disable_transition(struct mhi_controller *mhi_cntrl,dev_dbg (dev,"Waiting for all pending threads to complete\n" );wake_up_all (&mhi_cntrl->state_event);flush_work (&mhi_cntrl->st_worker); -flush_work (&mhi_cntrl->fw_worker);dev_dbg (dev,"Reset all active channels and remove MHI devices\n" );device_for_each_child (mhi_cntrl->cntrl_dev,NULL , mhi_destroy_device);
@@ -643,7 +642,7 @@ void mhi_pm_st_worker(struct work_struct *work) mhi_cntrl->ee =mhi_get_exec_env (mhi_cntrl);write_unlock_irq (&mhi_cntrl->pm_lock);if (MHI_IN_PBL (mhi_cntrl->ee)) -wake_up_all (&mhi_cntrl->state_event); +mhi_fw_load_handler (mhi_cntrl);break ;case DEV_ST_TRANSITION_SBL :write_lock_irq (&mhi_cntrl->pm_lock);
@@ -833,9 +832,6 @@ int mhi_async_power_up(struct mhi_controller *mhi_cntrl) next_state =MHI_IN_PBL (current_ee) ?DEV_ST_TRANSITION_PBL :DEV_ST_TRANSITION_READY ; -if (next_state ==DEV_ST_TRANSITION_PBL ) -schedule_work (&mhi_cntrl->fw_worker); -mhi_queue_state_transition (mhi_cntrl, next_state);mutex_unlock (&mhi_cntrl->pm_mutex);
include/linux/mhi.h +0 -2
@@ -331,7 +331,6 @@ struct mhi_controller_config {* @wlock: Lock for protecting device wakeup * @mhi_link_info: Device bandwidth info * @st_worker: State transition worker -* @fw_worker: Firmware download worker * @syserr_worker: System error worker * @state_event: State change event * @status_cb: CB function to notify power states of the device (required)
@@ -412,7 +411,6 @@ struct mhi_controller {spinlock_t wlock;struct mhi_link_info mhi_link_info;struct work_struct st_worker; -struct work_struct fw_worker;struct work_struct syserr_worker;wait_queue_head_t state_event;
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 6/14 bus: mhi: core: Return appropriate error codes for AMSS load failure
In reply to [email protected]From: Bhaumik Bhatt <[email protected]>
When loading AMSS firmware using BHIe protocol, return -ETIMEDOUT if no response is received within the timeout or return -EIO in case of a protocol returned failure or an MHI error state.
--- drivers/bus/mhi/core/boot.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
drivers/bus/mhi/core/boot.c +12 -11
@@ -176,6 +176,7 @@ static int mhi_fw_load_amss(struct mhi_controller *mhi_cntrl,void __iomem *base = mhi_cntrl->bhie;rwlock_t *pm_lock = &mhi_cntrl->pm_lock;u32 tx_status, sequence_id; +int ret;read_lock_bh (pm_lock);if (!MHI_REG_ACCESS_VALID (mhi_cntrl->pm_state)) {
@@ -198,19 +199,19 @@ static int mhi_fw_load_amss(struct mhi_controller *mhi_cntrl,read_unlock_bh (pm_lock);/* Wait for the image download to complete */ -wait_event_timeout (mhi_cntrl->state_event, -MHI_PM_IN_ERROR_STATE (mhi_cntrl->pm_state) || -mhi_read_reg_field (mhi_cntrl, base, -BHIE_TXVECSTATUS_OFFS , -BHIE_TXVECSTATUS_STATUS_BMSK , -BHIE_TXVECSTATUS_STATUS_SHFT , - &tx_status) || tx_status, -msecs_to_jiffies (mhi_cntrl->timeout_ms)); - -if (MHI_PM_IN_ERROR_STATE (mhi_cntrl->pm_state)) + ret =wait_event_timeout (mhi_cntrl->state_event, +MHI_PM_IN_ERROR_STATE (mhi_cntrl->pm_state) || +mhi_read_reg_field (mhi_cntrl, base, +BHIE_TXVECSTATUS_OFFS , +BHIE_TXVECSTATUS_STATUS_BMSK , +BHIE_TXVECSTATUS_STATUS_SHFT , + &tx_status) || tx_status, +msecs_to_jiffies (mhi_cntrl->timeout_ms)); +if (MHI_PM_IN_ERROR_STATE (mhi_cntrl->pm_state) || + tx_status !=BHIE_TXVECSTATUS_STATUS_XFER_COMPL )return -EIO ; -return (tx_status ==BHIE_TXVECSTATUS_STATUS_XFER_COMPL ) ?0 : -EIO ; +return (!ret) ? -ETIMEDOUT :0 ; } static int mhi_fw_load_sbl(struct mhi_controller *mhi_cntrl,
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 7/14 bus: mhi: core: Improve debug logs for loading firmware
In reply to [email protected]From: Bhaumik Bhatt <[email protected]>
Add log messages to track boot flow errors and timeouts in SBL or AMSS firmware loading to aid in debug.
--- drivers/bus/mhi/core/boot.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/bus/mhi/core/boot.c +18 -3
@@ -121,7 +121,8 @@ static int __mhi_download_rddm_in_panic(struct mhi_controller *mhi_cntrl) ee =mhi_get_exec_env (mhi_cntrl); } -dev_dbg (dev,"Waiting for image download completion, current EE: %s\n" , +dev_dbg (dev, +"Waiting for RDDM image download via BHIe, current EE:%s\n" ,TO_MHI_EXEC_STR (ee));while (retry--) {
@@ -152,11 +153,14 @@ static int __mhi_download_rddm_in_panic(struct mhi_controller *mhi_cntrl)int mhi_download_rddm_img (struct mhi_controller *mhi_cntrl,bool in_panic) {void __iomem *base = mhi_cntrl->bhie; +struct device *dev = &mhi_cntrl->mhi_dev->dev;u32 rx_status;if (in_panic)return __mhi_download_rddm_in_panic (mhi_cntrl); +dev_dbg (dev,"Waiting for RDDM image download via BHIe\n" ); +/* Wait for the image download to complete */ wait_event_timeout(mhi_cntrl->state_event, mhi_read_reg_field(mhi_cntrl, base,
@@ -174,6 +178,7 @@ static int mhi_fw_load_amss(struct mhi_controller *mhi_cntrl, const struct mhi_buf *mhi_buf) {void __iomem *base = mhi_cntrl->bhie; +struct device *dev = &mhi_cntrl->mhi_dev->dev;rwlock_t *pm_lock = &mhi_cntrl->pm_lock;u32 tx_status, sequence_id;int ret;
@@ -184,6 +189,7 @@ static int mhi_fw_load_amss(struct mhi_controller *mhi_cntrl,return -EIO ; } +dev_dbg (dev,"Starting AMSS download via BHIe\n" );mhi_write_reg (mhi_cntrl, base,BHIE_TXVECADDR_HIGH_OFFS ,upper_32_bits (mhi_buf->dma_addr));
@@ -435,7 +441,12 @@ void mhi_fw_load_handler(struct mhi_controller *mhi_cntrl)release_firmware (firmware);/* Error or in EDL mode, we're done */ -if (ret || mhi_cntrl->ee ==MHI_EE_EDL ) +if (ret) { +dev_err (dev,"MHI did not load SBL, ret:%d\n" , ret); +return ; + } + +if (mhi_cntrl->ee ==MHI_EE_EDL )return ;write_lock_irq (&mhi_cntrl->pm_lock);
@@ -463,8 +474,10 @@ void mhi_fw_load_handler(struct mhi_controller *mhi_cntrl)if (!mhi_cntrl->fbc_download)return ; -if (ret) +if (ret) { +dev_err (dev,"MHI did not enter READY state\n" ); goto error_read; + }/* Wait for the SBL event */ ret = wait_event_timeout(mhi_cntrl->state_event,
@@ -482,6 +495,8 @@ void mhi_fw_load_handler(struct mhi_controller *mhi_cntrl) ret =mhi_fw_load_amss (mhi_cntrl,/* Vector table is the last entry */ &image_info->mhi_buf[image_info->entries -1 ]); +if (ret) +dev_err (dev,"MHI did not load AMSS, ret:%d\n" , ret);release_firmware (firmware);
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 8/14 bus: mhi: core: Ensure non-zero session or sequence ID values are used
In reply to [email protected]From: Bhaumik Bhatt <[email protected]>
While writing any sequence or session identifiers, it is possible that the host could write a zero value, whereas only non-zero values should be supported writes to those registers. Ensure that the host does not write a non-zero value for them and also log them in debug messages. A macro is introduced to simplify this check and the existing checks are also converted to use this macro.
--- drivers/bus/mhi/core/boot.c | 15 +++++++-------- drivers/bus/mhi/core/internal.h | 1 + 2 files changed, 8 insertions(+), 8 deletions(-)
drivers/bus/mhi/core/boot.c +7 -8
@@ -43,10 +43,7 @@ void mhi_rddm_prepare(struct mhi_controller *mhi_cntrl, lower_32_bits(mhi_buf->dma_addr));mhi_write_reg (mhi_cntrl, base,BHIE_RXVECSIZE_OFFS , mhi_buf->len); - sequence_id =prandom_u32 () &BHIE_RXVECSTATUS_SEQNUM_BMSK ; - -if (unlikely (!sequence_id)) - sequence_id =1 ; + sequence_id =MHI_RANDOM_U32_NONZERO (BHIE_RXVECSTATUS_SEQNUM_BMSK ); mhi_write_reg_field(mhi_cntrl, base, BHIE_RXVECDB_OFFS, BHIE_RXVECDB_SEQNUM_BMSK, BHIE_RXVECDB_SEQNUM_SHFT,
@@ -189,7 +186,9 @@ static int mhi_fw_load_amss(struct mhi_controller *mhi_cntrl,return -EIO ; } -dev_dbg (dev,"Starting AMSS download via BHIe\n" ); + sequence_id =MHI_RANDOM_U32_NONZERO (BHIE_TXVECSTATUS_SEQNUM_BMSK ); +dev_dbg (dev,"Starting AMSS download via BHIe. Sequence ID:%u\n" , + sequence_id);mhi_write_reg (mhi_cntrl, base,BHIE_TXVECADDR_HIGH_OFFS ,upper_32_bits (mhi_buf->dma_addr));
@@ -198,7 +197,6 @@ static int mhi_fw_load_amss(struct mhi_controller *mhi_cntrl,mhi_write_reg (mhi_cntrl, base,BHIE_TXVECSIZE_OFFS , mhi_buf->len); - sequence_id =prandom_u32 () &BHIE_TXVECSTATUS_SEQNUM_BMSK ;mhi_write_reg_field (mhi_cntrl, base,BHIE_TXVECDB_OFFS ,BHIE_TXVECDB_SEQNUM_BMSK ,BHIE_TXVECDB_SEQNUM_SHFT , sequence_id);
@@ -246,14 +244,15 @@ static int mhi_fw_load_sbl(struct mhi_controller *mhi_cntrl, goto invalid_pm_state; } -dev_dbg (dev,"Starting SBL download via BHI\n" ); + session_id =MHI_RANDOM_U32_NONZERO (BHI_TXDB_SEQNUM_BMSK ); +dev_dbg (dev,"Starting SBL download via BHI. Session ID:%u\n" , + session_id);mhi_write_reg (mhi_cntrl, base,BHI_STATUS ,0 );mhi_write_reg (mhi_cntrl, base,BHI_IMGADDR_HIGH ,upper_32_bits (dma_addr));mhi_write_reg (mhi_cntrl, base,BHI_IMGADDR_LOW ,lower_32_bits (dma_addr));mhi_write_reg (mhi_cntrl, base,BHI_IMGSIZE , size); - session_id =prandom_u32 () &BHI_TXDB_SEQNUM_BMSK ;mhi_write_reg (mhi_cntrl, base,BHI_IMGTXDB , session_id);read_unlock_bh (pm_lock);
drivers/bus/mhi/core/internal.h +1 -0
@@ -452,6 +452,7 @@ enum mhi_pm_state {#define PRIMARY_CMD_RING 0#define MHI_DEV_WAKE_DB 127#define MHI_MAX_MTU 0xffff +#define MHI_RANDOM_U32_NONZERO (bmsk) (prandom_u32_max(bmsk) + 1)enum mhi_er_type {MHI_ER_TYPE_INVALID =0x0 ,
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 9/14 bus: mhi: core: Remove the system error worker thread
In reply to [email protected]From: Hemant Kumar <[email protected]>
Remove the system error worker thread and instead have the execution environment worker handle that transition to serialize processing and avoid any possible race conditions during shutdown.
--- drivers/bus/mhi/core/init.c | 2 +- drivers/bus/mhi/core/internal.h | 3 ++- drivers/bus/mhi/core/main.c | 6 +++--- drivers/bus/mhi/core/pm.c | 32 ++++++++++++++------------------ include/linux/mhi.h | 2 -- 5 files changed, 20 insertions(+), 25 deletions(-)
drivers/bus/mhi/core/init.c +1 -1
@@ -34,6 +34,7 @@ const char * const dev_state_tran_str[DEV_ST_TRANSITION_MAX] = { [DEV_ST_TRANSITION_READY] = "READY", [DEV_ST_TRANSITION_SBL] = "SBL", [DEV_ST_TRANSITION_MISSION_MODE] = "MISSION_MODE", + [DEV_ST_TRANSITION_SYS_ERR] = "SYS_ERR", };const char *const mhi_state_str[MHI_STATE_MAX ] = {
@@ -834,7 +835,6 @@ int mhi_register_controller(struct mhi_controller *mhi_cntrl,spin_lock_init (&mhi_cntrl->transition_lock);spin_lock_init (&mhi_cntrl->wlock);INIT_WORK (&mhi_cntrl->st_worker, mhi_pm_st_worker); -INIT_WORK (&mhi_cntrl->syserr_worker, mhi_pm_sys_err_worker);init_waitqueue_head (&mhi_cntrl->state_event); mhi_cmd = mhi_cntrl->mhi_cmd;
drivers/bus/mhi/core/internal.h +2 -1
@@ -386,6 +386,7 @@ enum dev_st_transition {DEV_ST_TRANSITION_READY ,DEV_ST_TRANSITION_SBL ,DEV_ST_TRANSITION_MISSION_MODE , +DEV_ST_TRANSITION_SYS_ERR , DEV_ST_TRANSITION_MAX, };
@@ -587,7 +588,7 @@ enum mhi_ee_type mhi_get_exec_env(struct mhi_controller *mhi_cntrl);int mhi_queue_state_transition (struct mhi_controller *mhi_cntrl,enum dev_st_transition state);void mhi_pm_st_worker (struct work_struct *work); -void mhi_pm_sys_err_worker (struct work_struct *work); +void mhi_pm_sys_err_handler (struct mhi_controller *mhi_cntrl);void mhi_fw_load_worker (struct work_struct *work);int mhi_ready_state_transition (struct mhi_controller *mhi_cntrl);void mhi_ctrl_ev_task (unsigned long data);
drivers/bus/mhi/core/main.c +3 -3
@@ -405,7 +405,7 @@ irqreturn_t mhi_intvec_threaded_handler(int irq_number, void *dev)if (MHI_IN_PBL (ee)) mhi_cntrl->status_cb (mhi_cntrl,MHI_CB_FATAL_ERROR );else -schedule_work (&mhi_cntrl->syserr_worker); +mhi_pm_sys_err_handler (mhi_cntrl); } exit_intvec:
@@ -733,7 +733,7 @@ int mhi_process_ctrl_ev_ring(struct mhi_controller *mhi_cntrl, MHI_PM_SYS_ERR_DETECT);write_unlock_irq (&mhi_cntrl->pm_lock);if (new_state ==MHI_PM_SYS_ERR_DETECT ) -schedule_work (&mhi_cntrl->syserr_worker); +mhi_pm_sys_err_handler (mhi_cntrl);break ; }default :
@@ -919,7 +919,7 @@ void mhi_ctrl_ev_task(unsigned long data) }write_unlock_irq (&mhi_cntrl->pm_lock);if (pm_state ==MHI_PM_SYS_ERR_DETECT ) -schedule_work (&mhi_cntrl->syserr_worker); +mhi_pm_sys_err_handler (mhi_cntrl); } }
drivers/bus/mhi/core/pm.c +14 -18
@@ -449,19 +449,8 @@ static void mhi_pm_disable_transition(struct mhi_controller *mhi_cntrl, to_mhi_pm_state_str(transition_state));/* We must notify MHI control driver so it can clean up first */ -if (transition_state ==MHI_PM_SYS_ERR_PROCESS ) { -/* -* If controller supports RDDM, we do not process -* SYS error state, instead we will jump directly -* to RDDM state -*/ -if (mhi_cntrl->rddm_image) { -dev_dbg (dev, -"Controller supports RDDM, so skip SYS_ERR\n" ); -return ; - } +if (transition_state ==MHI_PM_SYS_ERR_PROCESS ) mhi_cntrl->status_cb (mhi_cntrl,MHI_CB_SYS_ERROR ); - }mutex_lock (&mhi_cntrl->pm_mutex);write_lock_irq (&mhi_cntrl->pm_lock);
@@ -527,7 +516,6 @@ static void mhi_pm_disable_transition(struct mhi_controller *mhi_cntrl,mutex_unlock (&mhi_cntrl->pm_mutex);dev_dbg (dev,"Waiting for all pending threads to complete\n" );wake_up_all (&mhi_cntrl->state_event); -flush_work (&mhi_cntrl->st_worker);dev_dbg (dev,"Reset all active channels and remove MHI devices\n" );device_for_each_child (mhi_cntrl->cntrl_dev,NULL , mhi_destroy_device);
@@ -607,13 +595,17 @@ int mhi_queue_state_transition(struct mhi_controller *mhi_cntrl, }/* SYS_ERR worker */ -void mhi_pm_sys_err_worker (struct work_struct *work) +void mhi_pm_sys_err_handler (struct mhi_controller *mhi_cntrl) { -struct mhi_controller *mhi_cntrl =container_of (work, - struct mhi_controller, - syserr_worker); +struct device *dev = &mhi_cntrl->mhi_dev->dev; + +/* skip if controller supports RDDM */ +if (mhi_cntrl->rddm_image) { +dev_dbg (dev,"Controller supports RDDM, skip SYS_ERROR\n" ); +return ; + } -mhi_pm_disable_transition (mhi_cntrl,MHI_PM_SYS_ERR_PROCESS ); +mhi_queue_state_transition (mhi_cntrl,DEV_ST_TRANSITION_SYS_ERR ); }/* Device State Transition worker */
@@ -661,6 +653,10 @@ void mhi_pm_st_worker(struct work_struct *work)case DEV_ST_TRANSITION_READY :mhi_ready_state_transition (mhi_cntrl);break ; +case DEV_ST_TRANSITION_SYS_ERR : +mhi_pm_disable_transition + (mhi_cntrl,MHI_PM_SYS_ERR_PROCESS ); +break ;default :break ; }
include/linux/mhi.h +0 -2
@@ -331,7 +331,6 @@ struct mhi_controller_config {* @wlock: Lock for protecting device wakeup * @mhi_link_info: Device bandwidth info * @st_worker: State transition worker -* @syserr_worker: System error worker * @state_event: State change event * @status_cb: CB function to notify power states of the device (required) * @wake_get: CB function to assert device wake (optional)
@@ -411,7 +410,6 @@ struct mhi_controller {spinlock_t wlock;struct mhi_link_info mhi_link_info;struct work_struct st_worker; -struct work_struct syserr_worker;wait_queue_head_t state_event; void (*status_cb)(struct mhi_controller *mhi_cntrl,
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 10/14 bus: mhi: core: Handle disable transitions in state worker
In reply to [email protected]From: Hemant Kumar <[email protected]>
Mission mode transition is handled by state worker thread but power off is not. There is a possibility while mission mode transition is in progress which calls MHI client driver probe, power off is issued by MHI controller. This results into client driver probe and remove running in parallel and causes use after free situation. By queuing disable transition work when mission mode is in progress prevents the race condition.
--- drivers/bus/mhi/core/init.c | 1 + drivers/bus/mhi/core/internal.h | 1 + drivers/bus/mhi/core/pm.c | 11 ++++++++++- 3 files changed, 12 insertions(+), 1 deletion(-)
drivers/bus/mhi/core/init.c +1 -0
@@ -35,6 +35,7 @@ const char * const dev_state_tran_str[DEV_ST_TRANSITION_MAX] = { [DEV_ST_TRANSITION_SBL] = "SBL", [DEV_ST_TRANSITION_MISSION_MODE] = "MISSION_MODE", [DEV_ST_TRANSITION_SYS_ERR] = "SYS_ERR", + [DEV_ST_TRANSITION_DISABLE] = "DISABLE", };const char *const mhi_state_str[MHI_STATE_MAX ] = {
drivers/bus/mhi/core/internal.h +1 -0
@@ -387,6 +387,7 @@ enum dev_st_transition {DEV_ST_TRANSITION_SBL ,DEV_ST_TRANSITION_MISSION_MODE ,DEV_ST_TRANSITION_SYS_ERR , +DEV_ST_TRANSITION_DISABLE , DEV_ST_TRANSITION_MAX, };
drivers/bus/mhi/core/pm.c +10 -1
@@ -657,6 +657,10 @@ void mhi_pm_st_worker(struct work_struct *work)mhi_pm_disable_transition (mhi_cntrl,MHI_PM_SYS_ERR_PROCESS );break ; +case DEV_ST_TRANSITION_DISABLE : +mhi_pm_disable_transition + (mhi_cntrl,MHI_PM_SHUTDOWN_PROCESS ); +break ;default :break ; }
@@ -868,7 +872,12 @@ void mhi_power_down(struct mhi_controller *mhi_cntrl, bool graceful)to_mhi_pm_state_str (MHI_PM_LD_ERR_FATAL_DETECT ), to_mhi_pm_state_str(mhi_cntrl->pm_state)); } -mhi_pm_disable_transition (mhi_cntrl,MHI_PM_SHUTDOWN_PROCESS ); + +mhi_queue_state_transition (mhi_cntrl,DEV_ST_TRANSITION_DISABLE ); + +/* Wait for shutdown to complete */ +flush_work (&mhi_cntrl->st_worker); +mhi_deinit_free_irq (mhi_cntrl);if (!mhi_cntrl->pre_init) {
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 11/14 bus: mhi: core: Skip handling BHI irq if MHI reg access is not allowed
In reply to [email protected]From: Hemant Kumar <[email protected]>
Driver continues handling of BHI interrupt even if MHI register access is not allowed. By doing so it calls the status call back and performs early notification for the MHI client. This is not needed when MHI register access is not allowed. Hence skip the handling in this case and return. Also add debug log to print device state, local EE and device EE when reg access is valid.
--- drivers/bus/mhi/core/main.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-)
drivers/bus/mhi/core/main.c +14 -7
@@ -368,22 +368,29 @@ irqreturn_t mhi_irq_handler(int irq_number, void *dev)return IRQ_HANDLED ; } -irqreturn_t mhi_intvec_threaded_handler (int irq_number,void *dev) +irqreturn_t mhi_intvec_threaded_handler (int irq_number,void *priv) { -struct mhi_controller *mhi_cntrl = dev; +struct mhi_controller *mhi_cntrl = priv; +struct device *dev = &mhi_cntrl->mhi_dev->dev;enum mhi_state state =MHI_STATE_MAX ;enum mhi_pm_state pm_state =0 ;enum mhi_ee_type ee =0 ;write_lock_irq (&mhi_cntrl->pm_lock); -if (MHI_REG_ACCESS_VALID (mhi_cntrl->pm_state)) { - state =mhi_get_mhi_state (mhi_cntrl); - ee = mhi_cntrl->ee; - mhi_cntrl->ee =mhi_get_exec_env (mhi_cntrl); +if (!MHI_REG_ACCESS_VALID (mhi_cntrl->pm_state)) { +write_unlock_irq (&mhi_cntrl->pm_lock); + goto exit_intvec; } + state =mhi_get_mhi_state (mhi_cntrl); + ee = mhi_cntrl->ee; + mhi_cntrl->ee =mhi_get_exec_env (mhi_cntrl); +dev_dbg (dev,"local ee:%s device ee:%s dev_state:%s\n" , +TO_MHI_EXEC_STR (mhi_cntrl->ee),TO_MHI_EXEC_STR (ee), +TO_MHI_STATE_STR (state)); +if (state ==MHI_STATE_SYS_ERR ) { -dev_dbg (&mhi_cntrl->mhi_dev->dev,"System error detected\n" ); +dev_dbg (dev,"System error detected\n" ); pm_state =mhi_tryset_pm_state (mhi_cntrl,MHI_PM_SYS_ERR_DETECT ); }
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 12/14 bus: mhi: core: Do not process SYS_ERROR if RDDM is supported
In reply to [email protected]From: Hemant Kumar <[email protected]>
Devices that support RDDM do not require processing SYS_ERROR as it is deemed redundant. Avoid SYS_ERROR processing if RDDM is supported by the device.
--- drivers/bus/mhi/core/main.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/bus/mhi/core/main.c +8 -3
@@ -396,9 +396,9 @@ irqreturn_t mhi_intvec_threaded_handler(int irq_number, void *priv) }write_unlock_irq (&mhi_cntrl->pm_lock); -/* If device in RDDM don't bother processing SYS error */ -if (mhi_cntrl->ee ==MHI_EE_RDDM ) { -if (mhi_cntrl->ee != ee) { +/* If device supports RDDM don't bother processing SYS error */ +if (mhi_cntrl->rddm_image) { +if (mhi_cntrl->ee ==MHI_EE_RDDM && mhi_cntrl->ee != ee) { mhi_cntrl->status_cb (mhi_cntrl,MHI_CB_EE_RDDM );wake_up_all (&mhi_cntrl->state_event); }
@@ -734,6 +734,11 @@ int mhi_process_ctrl_ev_ring(struct mhi_controller *mhi_cntrl, {enum mhi_pm_state new_state; +/* skip SYS_ERROR handling if RDDM supported */ +if (mhi_cntrl->ee ==MHI_EE_RDDM || + mhi_cntrl->rddm_image) +break ; +dev_dbg (dev,"System error detected\n" );write_lock_irq (&mhi_cntrl->pm_lock); new_state = mhi_tryset_pm_state(mhi_cntrl,
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 13/14 bus: mhi: core: Handle write lock properly in mhi_pm_m0_transition
In reply to [email protected]From: Hemant Kumar <[email protected]>
Take write lock only to protect db_mode member of mhi channel. This allows rest of the mhi channels to just take read lock which fine grains the locking. It prevents channel readers to starve if they try to enter critical section after a writer.
--- drivers/bus/mhi/core/pm.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/bus/mhi/core/pm.c +7 -3
@@ -288,14 +288,18 @@ int mhi_pm_m0_transition(struct mhi_controller *mhi_cntrl)for (i =0 ; i < mhi_cntrl->max_chan; i++, mhi_chan++) {struct mhi_ring *tre_ring = &mhi_chan->tre_ring; -write_lock_irq (&mhi_chan->lock); -if (mhi_chan->db_cfg.reset_req) +if (mhi_chan->db_cfg.reset_req) { +write_lock_irq (&mhi_chan->lock); mhi_chan->db_cfg.db_mode = true; +write_unlock_irq (&mhi_chan->lock); + } + +read_lock_irq (&mhi_chan->lock);/* Only ring DB if ring is not empty */ if (tre_ring->base && tre_ring->wp != tre_ring->rp)mhi_ring_chan_db (mhi_cntrl, mhi_chan); -write_unlock_irq (&mhi_chan->lock); +read_unlock_irq (&mhi_chan->lock); } mhi_cntrl->wake_put (mhi_cntrl, false);
Signature · 1 line
2.26.GIT
[email protected]
maintainerPATCH 14/14 bus: mhi: core: Handle syserr during power_up
In reply to [email protected]From: Jeffrey Hugo <[email protected]>
The MHI device may be in the syserr state when we attempt to init it in power_up(). Since we have no local state, the handling is simple - reset the device and wait for it to transition out of the reset state.
--- drivers/bus/mhi/core/pm.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
drivers/bus/mhi/core/pm.c +27 -0
@@ -763,6 +763,7 @@ static void mhi_deassert_dev_wake(struct mhi_controller *mhi_cntrl,int mhi_async_power_up (struct mhi_controller *mhi_cntrl) { +enum mhi_state state;enum mhi_ee_type current_ee;enum dev_st_transition next_state;struct device *dev = &mhi_cntrl->mhi_dev->dev;
@@ -832,6 +833,32 @@ int mhi_async_power_up(struct mhi_controller *mhi_cntrl) goto error_bhi_offset; } + state =mhi_get_mhi_state (mhi_cntrl); +if (state ==MHI_STATE_SYS_ERR ) { +mhi_set_mhi_state (mhi_cntrl,MHI_STATE_RESET ); + ret =wait_event_timeout (mhi_cntrl->state_event, +MHI_PM_IN_FATAL_STATE (mhi_cntrl->pm_state) || +mhi_read_reg_field (mhi_cntrl, + mhi_cntrl->regs, +MHICTRL , +MHICTRL_RESET_MASK , +MHICTRL_RESET_SHIFT , + &val) || + !val, +msecs_to_jiffies (mhi_cntrl->timeout_ms)); +if (ret) { + ret = -EIO ; +dev_info (dev,"Failed to reset MHI due to syserr state\n" ); + goto error_bhi_offset; + } + +/* +* device cleares INTVEC as part of RESET processing, +* re-program it +*/ +mhi_write_reg (mhi_cntrl, mhi_cntrl->bhi,BHI_INTVEC ,0 ); + } +/* Transition to next state */ next_state =MHI_IN_PBL (current_ee) ?DEV_ST_TRANSITION_PBL :DEV_ST_TRANSITION_READY ;
Signature · 1 line
2.26.GIT
Quoted 8 lines · On Thu, May 21, 2020 at 08:55:26PM +0530, [email protected] wrote:
From: Manivannan Sadhasivam <[email protected]>
Hi Greg,
Here is the set of MHI patches for v5.8. Most of the patches are cleanup and refactoring ones. All of them are reviewed by myself and Jeff and also verified on x86 and ARM64 architectures for functionality.
Greg,
Sorry, something wrong happened with my git config and these patches were sent from my korg ID. Please let me know if I have to resend from my linaro ID to match the signed-off-by tag.
Sorry for the confusion!
Thanks,
Mani
Quoted 83 lines
Here is the short summary:
-------------------------------------------------------------- The firmware download was handled by a worker thread which gets scheduled when the device powers up. But this thread waits until the device gets into PBL state (notified using PM state worker). Sometimes, there might be delay for the device to enter PBL state and due to that the firmware worker thread will timeout. So in order to handle this situation effectively, the firmware load is now directly called by PM state worker instead of scheduling the thread.
- Return proper error codes incase of error while loading the AMSS firmware through BHIE protocol
- The MHI register space of the device accepts only non-zero values for the sequence identifier. But there is a possibility that the host might write zero (due to the use of
prandom_u32()API). Hence, a macro is introduced which provides non-zero sequence identifiers and used them in all places.- Moved all common TRE generation code to
mhi_gen_tre()function- The MHI host reads channel ID from the event ring element of the client device. This ID can be of any value between 0 to 255 but the host may not support all those IDs. So reject the event ring elements whose channel IDs are not within the limits of the controller.
- Limit the transfer length read from the client device. This value should be within the size of the MHI host buffer but there are chances this can be larger.
- Remove the system worker thread for processing the
SYS_ERRcondition and instead call the function directly from EE worker. This is done to avoid any possible race while MHI shutting down.- Handle MHI power off in the state worker thread as like
MISSION_MODE. This helps in preventing a possible race condition where a power off is issued by the controller while processing mission mode.- Skip the handling of BHI interrupt when the register access is not allowed due to the device in wrong PM state.
- The
write_lockof 'mhi_chan->lock' should only protect 'db_mode'. Hence, use it properly in places where it is protecting other unwanted regions.- Reset the client device if it is in
SYS_ERRstate during power up.-------------------------------------------------------------
Please consider merging!
Thanks,
ManiBhaumik Bhatt (4):
bus: mhi: core: Handle firmware load using state worker
bus: mhi: core: Return appropriate error codes for AMSS load failure
bus: mhi: core: Improve debug logs for loading firmware
bus: mhi: core: Ensure non-zero session or sequence ID values are usedHemant Kumar (9):
bus: mhi: core: Refactor mhi queue APIs
bus: mhi: core: Cache intmod from mhi event to mhi channel
bus: mhi: core: Add range check for channel id received in event ring
bus: mhi: core: Read transfer length from an event properly
bus: mhi: core: Remove the system error worker thread
bus: mhi: core: Handle disable transitions in state worker
bus: mhi: core: Skip handling BHI irq if MHI reg access is not allowed
bus: mhi: core: Do not processSYS_ERRORif RDDM is supported
bus: mhi: core: Handle write lock properly inmhi_pm_m0_transitionJeffrey Hugo (1):
bus: mhi: core: Handle syserr duringpower_updrivers/bus/mhi/core/boot.c | 75 ++++++------ drivers/bus/mhi/core/init.c | 8 +- drivers/bus/mhi/core/internal.h | 9 +- drivers/bus/mhi/core/main.c | 194 ++++++++++++++++++-------------- drivers/bus/mhi/core/pm.c | 86 +++++++++----- include/linux/mhi.h | 4 - 6 files changed, 217 insertions(+), 159 deletions(-)Signature · 1 line
2.26.GIT
Quoted 10 lines · Manivannan Sadhasivam
Quoted 8 lines · On Thu, May 21, 2020 at 08:55:26PM +0530, [email protected] wrote:
From: Manivannan Sadhasivam <[email protected]>
Hi Greg,
Here is the set of MHI patches for v5.8. Most of the patches are cleanup and refactoring ones. All of them are reviewed by myself and Jeff and also verified on x86 and ARM64 architectures for functionality.
Quoted 5 lines
Greg,
Sorry, something wrong happened with my git config and these patches were sent from my korg ID. Please let me know if I have to resend from my linaro ID to match the signed-off-by tag.
Yes, please fix up and resend.
thanks,
greg k-h