* [PATCH v3] misc: fastrpc: Add cache maintenance for non-coherent platforms
@ 2026-11-24 12:06 Abhinav Parihar
2026-11-24 12:29 ` Arnd Bergmann
0 siblings, 1 reply; 2+ messages in thread
From: Abhinav Parihar @ 2026-11-24 12:06 UTC (permalink / raw)
To: srinivas.kandagatla, linux-arm-msm
Cc: Abhinav Parihar, gregkh, quic_bkumar, ekansh.gupta, linux-kernel,
quic_chennak, dri-devel, arnd
Some platforms using fastrpc do not support DMA coherency on
HLOS. On such systems, explicit cache maintenance is
required to ensure data consistency for RPC argument buffers.
Add cache maintenance for argument buffers when operating on
non-coherent platforms:
- Flush input buffers before invoking RPC to ensure CPU writes are
visible to the DSP
- Invalidate output buffers after RPC completion to ensure DSP
writes are visible to the CPU
Introduce helper functions fastrpc_flush_args() and
fastrpc_inv_args() to perform the required dma-buf cache
operations. These are invoked only when the device is not marked
as DMA coherent.
The coherency capability is determined using the "dma-coherent"
device tree property and stored per session context.
This ensures correct data synchronization on platforms lacking
DMA coherency, while avoiding unnecessary overhead on coherent
systems.
Signed-off-by: Abhinav Parihar <abhinav.parihar@oss.qualcomm.com>
---
Patch [v2]: https://lore.kernel.org/all/20260916185850.3686010-1-abhinav.parihar@oss.qualcomm.com/
Changes in v3:
- Fix missing max_raix update causes incorrect do_cmo evaluation.
- Use of_dma_is_coherent() to determine device coherency.
- Simplify input/output buffer classification and overlap checks for cache maintenance.
Changes in v2:
- Fix buffer overlap handling to ensure cache maintenance is performed
when input and output buffers overlap and the overlap calculation
suppresses the buffer's mstart/mend range.
- Add a do_cmo flag to track buffers that require cache maintenance
even when their overlap range is clipped.
- Invalidate output buffers before sending the RPC request, preventing
stale CPU cache lines from being written back while the DSP updates
the buffers.
- Use DMA_FROM_DEVICE consistently for both begin_cpu_access() and
end_cpu_access() when invalidating output buffers.
drivers/misc/fastrpc.c | 67 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 66 insertions(+), 1 deletion(-)
diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c
index 9e760f68bcaf..39ad0ab88aec 100644
--- a/drivers/misc/fastrpc.c
+++ b/drivers/misc/fastrpc.c
@@ -222,6 +222,7 @@ struct fastrpc_buf_overlap {
u64 mstart;
u64 mend;
u64 offset;
+ bool do_cmo;
};
struct fastrpc_buf {
@@ -297,6 +298,7 @@ struct fastrpc_session_ctx {
int sid;
bool used;
bool valid;
+ bool coherent;
};
struct fastrpc_soc_data {
@@ -666,7 +668,9 @@ static int olaps_cmp(const void *a, const void *b)
static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
{
u64 max_end = 0;
+ int max_raix = -1;
int i;
+ int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
for (i = 0; i < ctx->nbufs; ++i) {
ctx->olaps[i].start = ctx->args[i].ptr;
@@ -685,7 +689,10 @@ static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
if (ctx->olaps[i].end > max_end) {
max_end = ctx->olaps[i].end;
+ max_raix = ctx->olaps[i].raix;
} else {
+ if ((max_raix < inbufs) != (ctx->olaps[i].raix < inbufs))
+ ctx->olaps[i].do_cmo = true;
ctx->olaps[i].mend = 0;
ctx->olaps[i].mstart = 0;
}
@@ -695,6 +702,7 @@ static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
ctx->olaps[i].mstart = ctx->olaps[i].start;
ctx->olaps[i].offset = 0;
max_end = ctx->olaps[i].end;
+ max_raix = ctx->olaps[i].raix;
}
}
}
@@ -714,6 +722,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
INIT_LIST_HEAD(&ctx->node);
ctx->fl = user;
+ ctx->sc = sc;
ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
REMOTE_SCALARS_OUTBUFS(sc);
@@ -739,7 +748,6 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
/* Take a reference to user, released in fastrpc_context_free() */
fastrpc_user_get(user);
- ctx->sc = sc;
ctx->retval = -1;
ctx->pid = current->pid;
ctx->client_id = user->client_id;
@@ -1078,6 +1086,54 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
return 0;
}
+static void fastrpc_flush_args(struct fastrpc_invoke_ctx *ctx)
+{
+ union fastrpc_remote_arg *rpra = ctx->rpra;
+ int i, inbufs;
+
+ inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
+
+ for (i = 0; i < ctx->nbufs; ++i) {
+ int raix = ctx->olaps[i].raix;
+ struct fastrpc_map *map = ctx->maps[raix];
+
+ if (raix + 1 > inbufs)
+ continue;
+ if (!map || !map->buf)
+ continue;
+
+ if (rpra[raix].buf.len && (ctx->olaps[i].mstart || ctx->olaps[i].do_cmo)) {
+ dma_buf_begin_cpu_access(map->buf, DMA_TO_DEVICE);
+ dma_buf_end_cpu_access(map->buf, DMA_TO_DEVICE);
+ }
+ }
+}
+
+static void fastrpc_inv_args(struct fastrpc_invoke_ctx *ctx)
+{
+ union fastrpc_remote_arg *rpra = ctx->rpra;
+ int i, inbufs;
+
+ inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
+
+ for (i = 0; i < ctx->nbufs; ++i) {
+ int raix = ctx->olaps[i].raix;
+ struct fastrpc_map *map = ctx->maps[raix];
+
+ if (raix + 1 <= inbufs)
+ continue;
+ if (!rpra[raix].buf.len)
+ continue;
+ if (!map || !map->buf)
+ continue;
+
+ if (ctx->olaps[i].mstart || ctx->olaps[i].do_cmo) {
+ dma_buf_begin_cpu_access(map->buf, DMA_FROM_DEVICE);
+ dma_buf_end_cpu_access(map->buf, DMA_FROM_DEVICE);
+ }
+ }
+}
+
static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
{
return (struct fastrpc_invoke_buf *)(&pra[len]);
@@ -1202,6 +1258,9 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
}
}
+ if (!ctx->fl->sctx->coherent)
+ fastrpc_flush_args(ctx);
+
for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
list[i].num = ctx->args[i].length ? 1 : 0;
list[i].pgidx = i;
@@ -1387,6 +1446,8 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
if (err)
goto bail;
+ if (!fl->sctx->coherent)
+ fastrpc_inv_args(ctx);
/* make sure that all CPU memory writes are seen by DSP */
dma_wmb();
/* Send invoke buffer to remote dsp */
@@ -1407,6 +1468,9 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel,
/* make sure that all memory writes by DSP are seen by CPU */
dma_rmb();
+ if (!fl->sctx->coherent)
+ fastrpc_inv_args(ctx);
+
/* populate all the output buffers with results */
err = fastrpc_put_args(ctx, kernel);
if (err)
@@ -2370,6 +2434,7 @@ static int fastrpc_cb_init(struct platform_device *pdev)
sess->used = false;
sess->valid = true;
sess->dev = dev;
+ sess->coherent = of_dma_is_coherent(dev->of_node);
sess->sid = sid;
if (cctx->domain_id == CDSP_DOMAIN_ID)
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH v3] misc: fastrpc: Add cache maintenance for non-coherent platforms
2026-11-24 12:06 [PATCH v3] misc: fastrpc: Add cache maintenance for non-coherent platforms Abhinav Parihar
@ 2026-11-24 12:29 ` Arnd Bergmann
0 siblings, 0 replies; 2+ messages in thread
From: Arnd Bergmann @ 2026-11-24 12:29 UTC (permalink / raw)
To: Abhinav Parihar, Srinivas Kandagatla, linux-arm-msm
Cc: Greg Kroah-Hartman, quic_bkumar, Ekansh Gupta, linux-kernel,
quic_chennak, dri-devel
On Tue, Nov 24, 2026, at 13:06, Abhinav Parihar wrote:
> Some platforms using fastrpc do not support DMA coherency on
> HLOS. On such systems, explicit cache maintenance is
> required to ensure data consistency for RPC argument buffers.
>
> Add cache maintenance for argument buffers when operating on
> non-coherent platforms:
> - Flush input buffers before invoking RPC to ensure CPU writes are
> visible to the DSP
> - Invalidate output buffers after RPC completion to ensure DSP
> writes are visible to the CPU
>
> Introduce helper functions fastrpc_flush_args() and
> fastrpc_inv_args() to perform the required dma-buf cache
> operations. These are invoked only when the device is not marked
> as DMA coherent.
>
> The coherency capability is determined using the "dma-coherent"
> device tree property and stored per session context.
The dma-mapping abstraction is meant to hide this from drivers,
why does this driver need to know?
> @@ -1078,6 +1086,54 @@ static int fastrpc_create_maps(struct
> fastrpc_invoke_ctx *ctx)
> return 0;
> }
>
> +static void fastrpc_flush_args(struct fastrpc_invoke_ctx *ctx)
> +{
> + union fastrpc_remote_arg *rpra = ctx->rpra;
> + int i, inbufs;
> +
> + inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
> +
> + for (i = 0; i < ctx->nbufs; ++i) {
> + int raix = ctx->olaps[i].raix;
> + struct fastrpc_map *map = ctx->maps[raix];
> +
> + if (raix + 1 > inbufs)
> + continue;
> + if (!map || !map->buf)
> + continue;
> +
> + if (rpra[raix].buf.len && (ctx->olaps[i].mstart ||
> ctx->olaps[i].do_cmo)) {
> + dma_buf_begin_cpu_access(map->buf, DMA_TO_DEVICE);
> + dma_buf_end_cpu_access(map->buf, DMA_TO_DEVICE);
> + }
> + }
> +}
This does not look like an appropriate interface: the driver
should not have to worry about platform specific details
of 'flush' or 'invalidate' and only access the data between
'begin' and 'end' here. E.g. if there is an iommu or swiotlb,
the buffer may become inaccessible again after 'end'.
Can you change this to have separate loops to manage
ownership before and after the access?
Arnd
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 12:29 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 12:06 [PATCH v3] misc: fastrpc: Add cache maintenance for non-coherent platforms Abhinav Parihar
2026-11-24 12:29 ` Arnd Bergmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®