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
),
74 sg_set_buf(sg_ptr
, block
, PAGE_SIZE
);
80 /* See also: msc.c: __msc_buffer_win_free() */
81 static void msu_sink_free_window(void *data
, struct sg_table
*sgt
)
83 struct msu_sink_private
*priv
= data
;
84 struct scatterlist
*sg_ptr
;
87 for_each_sg(sgt
->sgl
, sg_ptr
, sgt
->nents
, i
) {
88 dma_free_coherent(priv
->dev
->parent
->parent
, PAGE_SIZE
,
89 sg_virt(sg_ptr
), sg_dma_address(sg_ptr
));
96 static int msu_sink_ready(void *data
, struct sg_table
*sgt
, size_t bytes
)
98 struct msu_sink_private
*priv
= data
;
100 intel_th_msc_window_unlock(priv
->dev
, sgt
);
105 static const struct msu_buffer sink_mbuf
= {
107 .assign
= msu_sink_assign
,
108 .unassign
= msu_sink_unassign
,
109 .alloc_window
= msu_sink_alloc_window
,
110 .free_window
= msu_sink_free_window
,
111 .ready
= msu_sink_ready
,
114 module_intel_th_msu_buffer(sink_mbuf
);
116 MODULE_LICENSE("GPL v2");