WIP FPC-III support
[linux/fpc-iii.git] / drivers / hwtracing / intel_th / msu-sink.c
blob2c7f5116be126609714bb5f290289447b8b40d76
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * An example software sink buffer for Intel TH MSU.
5 * Copyright (C) 2019 Intel Corporation.
6 */
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>
14 #define MAX_SGTS 16
16 struct msu_sink_private {
17 struct device *dev;
18 struct sg_table **sgts;
19 unsigned int nr_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);
27 if (!priv)
28 return NULL;
30 priv->sgts = kcalloc(MAX_SGTS, sizeof(void *), GFP_KERNEL);
31 if (!priv->sgts) {
32 kfree(priv);
33 return NULL;
36 priv->dev = dev;
37 *mode = MSC_MODE_MULTI;
39 return priv;
42 static void msu_sink_unassign(void *data)
44 struct msu_sink_private *priv = data;
46 kfree(priv->sgts);
47 kfree(priv);
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;
54 unsigned int nents;
55 struct scatterlist *sg_ptr;
56 void *block;
57 int ret, i;
59 if (priv->nr_sgts == MAX_SGTS)
60 return -ENOMEM;
62 nents = DIV_ROUND_UP(size, PAGE_SIZE);
64 ret = sg_alloc_table(*sgt, nents, GFP_KERNEL);
65 if (ret)
66 return -ENOMEM;
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),
73 GFP_KERNEL);
74 sg_set_buf(sg_ptr, block, PAGE_SIZE);
77 return nents;
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;
85 int i;
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));
92 sg_free_table(sgt);
93 priv->nr_sgts--;
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);
102 return 0;
105 static const struct msu_buffer sink_mbuf = {
106 .name = "sink",
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");