bus: mhi: Ensure correct ring update ordering with memory barrier
Subsystems (2)
PATCH v2 1/1 bus: mhi: Ensure correct ring update ordering with memory barrier
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (dma_wmb).
---
v2: fix comment style
drivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/bus/mhi/core/main.c +7 -0
@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db; db = ring->iommu_base + (ring->wp - ring->base); + +/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db; + mhi_chan->db_cfg.process_db (mhi_cntrl, &mhi_chan->db_cfg, ring->db_addr, db); }
Signature · 1 line
2.7.4
Quoted 21 lines · Loic Poulain
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>---
v2: fix comment styledrivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/bus/mhi/core/main.c +1
-0@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db; db = ring->iommu_base + (ring->wp - ring->base); +
drivers/bus/mhi/core/main.c void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl, · line 124
Quoted 10 lines
drivers/bus/mhi/core/main.c +6
-0+/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db; + mhi_chan->db_cfg.process_db (mhi_cntrl, &mhi_chan->db_cfg, ring->db_addr, db); }
Do we care about the ordering between updating ctxt_wp and the doorbell?
As far as I am aware common device implementations only use the value
from the doorbell, but I wonder if that changes, could we run into issues?
Signature · 3 lines
Jeffrey Hugo Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 16 lines · Loic Poulain
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>---
v2: fix comment styledrivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/bus/mhi/core/main.c +0
-0
drivers/bus/mhi/core/main.c void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl, · line 120
Quoted 10 lines
drivers/bus/mhi/core/main.c +6
-0@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db; db = ring->iommu_base + (ring->wp - ring->base); + +/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db;
As Jeff pointed out, the barrier should come after updating ctxt_wp.
Thanks,
Mani
Quoted 7 lines
drivers/bus/mhi/core/main.c +1
-0+ mhi_chan->db_cfg.process_db (mhi_cntrl, &mhi_chan->db_cfg, ring->db_addr, db); }Signature · 1 line
2.7.4
Quoted 20 lines · Manivannan Sadhasivam
Quoted 18 lines · Loic Poulain
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>---
v2: fix comment styledrivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/bus/mhi/core/main.c +0
-0@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db;
Quoted 10 lines
drivers/bus/mhi/core/main.c void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl, · line 120
Quoted 8 lines
drivers/bus/mhi/core/main.c +6
-0db = ring->iommu_base + (ring->wp - ring->base); + +/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db;As Jeff pointed out, the barrier should come after updating
ctxt_wp.
Actually, device can poll for the write pointer (e.g. in burst mode), so we need to be sure the element data are written before writing this write pointer (since it can be accessed at any time on device side, not only after doorbell).
I think that what jeff pointed is that we also need to ensure that write pointer is also updated before we ring the doorbell (doorbell mode), but this is implicitly done by the MMIO writing (using writeX()) of the doorbell register.(cf
https://www.kernel.org/doc/Documentation/memory-barriers.txt).
Regards,
Loic
Quoted 11 lines
Thanks,
ManiQuoted 7 lines
drivers/bus/mhi/core/main.c +1
-1+ mhi_chan->db_cfg.process_db (mhi_cntrl, &mhi_chan->db_cfg, ring->db_addr, db); } --2.7.4
Quoted 38 lines · Loic Poulain
Quoted 20 lines · Manivannan Sadhasivam
Quoted 18 lines · Loic Poulain
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>---
v2: fix comment styledrivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/bus/mhi/core/main.c +0
-0@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db;Quoted 10 lines
drivers/bus/mhi/core/main.c void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl, · line 120
Quoted 8 lines
drivers/bus/mhi/core/main.c +6
-0db = ring->iommu_base + (ring->wp - ring->base); + +/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db;As Jeff pointed out, the barrier should come after updating
ctxt_wp.Actually, device can poll for the write pointer (e.g. in burst mode), so we need to be sure the element data are written before writing this write pointer (since it can be accessed at any time on device side, not only after doorbell).
Quoted 5 lines
I think that what jeff pointed is that we also need to ensure that write pointer is also updated before we ring the doorbell (doorbell mode), but this is implicitly done by the MMIO writing (using
writeX()) of the doorbell register.(cf
https://www.kernel.org/doc/Documentation/memory-barriers.txt).
If we are using the version of the writeX API that includes a barrier, then that would be sufficient.
Signature · 3 lines
Jeffrey Hugo Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 19 lines · Manivannan Sadhasivam
Quoted 18 lines · Loic Poulain
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>---
v2: fix comment styledrivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/bus/mhi/core/main.c +0
-0@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db;
Quoted 10 lines
drivers/bus/mhi/core/main.c void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl, · line 120
Quoted 8 lines
drivers/bus/mhi/core/main.c +6
-0db = ring->iommu_base + (ring->wp - ring->base); + +/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db;As Jeff pointed out, the barrier should come after updating
ctxt_wp.
Actually, you potentially need both. The write to the ring element needs to hit the memory before the content write pointer is updated, since the context write pointer is making the ring element "visible" to the device. Then the context write pointer needs to hit memory before the doorbell is updated since the doorbell makes the pointer "visible" to the device.
Signature · 3 lines
Jeffrey Hugo Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 5 lines · Loic Poulain
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>
Thanks,
Mani
Quoted 28 lines
---
v2: fix comment styledrivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/bus/mhi/core/main.c +7
-0@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db; db = ring->iommu_base + (ring->wp - ring->base); + +/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db; + mhi_chan->db_cfg.process_db (mhi_cntrl, &mhi_chan->db_cfg, ring->db_addr, db); }Signature · 1 line
2.7.4
Manivannan Sadhasivam
AppliedQuoted 5 lines · Loic Poulain
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>
Applied to mhi-next!
Thanks,
Mani
Quoted 28 lines
---
v2: fix comment styledrivers/bus/mhi/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/bus/mhi/core/main.c +7
-0@@ -111,7 +111,14 @@ void mhi_ring_chan_db(struct mhi_controller *mhi_cntrl,dma_addr_t db; db = ring->iommu_base + (ring->wp - ring->base); + +/* +* Writes to the new ring element must be visible to the hardware +* before letting h/w know there is new element to fetch. +*/ +dma_wmb (); *ring->ctxt_wp = db; + mhi_chan->db_cfg.process_db (mhi_cntrl, &mhi_chan->db_cfg, ring->db_addr, db); }Signature · 1 line
2.7.4
This patch was applied to qcom/linux.git (refs/heads/for-next):
Hello:
This patch was applied to qcom/linux.git (refs/heads/for-next):
Quoted 9 lines · On Thu, 26 Nov 2020 16:06:41 +0100 you wrote:
The ring element data, though being part of coherent memory, still need to be performed before updating the ring context to point to this new element. That can be guaranteed with a memory barrier (
dma_wmb).Signed-off-by Loic Poulain <[email protected]>---
v2: fix comment style[...]
Here is the summary with links:
- [v2] bus: mhi: Ensure correct ring update ordering with memory barrier
https://git.kernel.org/qcom/c/f49b6aeb5c45
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html