1 // SPDX-License-Identifier: GPL-2.0
3 * An example software sink buffer for Intel TH MSU.
5 * Copyright (C) 2019 Intel Corporation.
8 #include <linux/intel_th.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
16 struct msu_sink_private
{
18 struct sg_table
**sgts
;
22 static void *msu_sink_assign(struct device
*dev
, int *mode
)
24 struct msu_sink_private
*priv
;
26 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
30 priv
->sgts
= kcalloc(MAX_SGTS
, sizeof(void *), GFP_KERNEL
);
37 *mode
= MSC_MODE_MULTI
;
42 static void msu_sink_unassign(void *data
)
44 struct msu_sink_private
*priv
= data
;
50 /* See also: msc.c: __msc_buffer_win_alloc() */
51 static int msu_sink_alloc_window(void *data
, struct sg_table
**sgt
, size_t size
)
53 struct msu_sink_private
*priv
= data
;
55 struct scatterlist
*sg_ptr
;
59 if (priv
->nr_sgts
== MAX_SGTS
)
62 nents
= DIV_ROUND_UP(size
, PAGE_SIZE
);
64 ret
= sg_alloc_table(*sgt
, nents
, GFP_KERNEL
);
68 priv
->sgts
[priv
->nr_sgts
++] = *sgt
;
70 for_each_sg((*sgt
)->sgl
, sg_ptr
, nents
, i
) {
71 block
= dma_alloc_coherent(priv
->dev
->parent
->parent
,
72 PAGE_SIZE
, &sg_dma_address(sg_ptr
),
77 sg_set_buf(sg_ptr
, block
, PAGE_SIZE
);
83 /* See also: msc.c: __msc_buffer_win_free() */
84 static void msu_sink_free_window(void *data
, struct sg_table
*sgt
)
86 struct msu_sink_private
*priv
= data
;
87 struct scatterlist
*sg_ptr
;
90 for_each_sg(sgt
->sgl
, sg_ptr
, sgt
->nents
, i
) {
91 dma_free_coherent(priv
->dev
->parent
->parent
, PAGE_SIZE
,
92 sg_virt(sg_ptr
), sg_dma_address(sg_ptr
));
99 static int msu_sink_ready(void *data
, struct sg_table
*sgt
, size_t bytes
)
101 struct msu_sink_private
*priv
= data
;
103 intel_th_msc_window_unlock(priv
->dev
, sgt
);
108 static const struct msu_buffer sink_mbuf
= {
110 .assign
= msu_sink_assign
,
111 .unassign
= msu_sink_unassign
,
112 .alloc_window
= msu_sink_alloc_window
,
113 .free_window
= msu_sink_free_window
,
114 .ready
= msu_sink_ready
,
117 module_intel_th_msu_buffer(sink_mbuf
);
119 MODULE_DESCRIPTION("example software sink buffer for Intel TH MSU");
120 MODULE_LICENSE("GPL v2");